Handle errors in tracing for environments that don't support Error.captureStackTrace (i.e. firefox) (#3623)

Error.captureStackTrace is used to get stacktrace for tracing details,
but it's not supported in some browser like firefox, so add handling for
it. thanks.

---------

Co-authored-by: Timothee Guerin <timothee.guerin@outlook.com>
This commit is contained in:
Rodge Fu 2024-06-21 09:01:38 +08:00 коммит произвёл GitHub
Родитель aed8ebc215
Коммит 224e725e43
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 18 добавлений и 3 удалений

Просмотреть файл

@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/compiler"
---
Fix crash of language server on firefox

Просмотреть файл

@ -48,9 +48,17 @@ export function createFileSystemCache({
changes = []; changes = [];
const r = cache.get(path); const r = cache.get(path);
if (!r) { if (!r) {
const target: any = {}; let callstack: string | undefined;
Error.captureStackTrace(target); try {
const callstack = target.stack.substring("Error\n".length); const target: any = {};
// some browser doesn't support Error.captureStackTrace (i.e. Firefox)
if (typeof Error.captureStackTrace === "function") {
Error.captureStackTrace(target);
callstack = target.stack.substring("Error\n".length);
}
} catch {
// just ignore the error, we don't want tracing error to impact normal functionality
}
log({ level: "trace", message: `FileSystemCache miss for ${path}`, detail: callstack }); log({ level: "trace", message: `FileSystemCache miss for ${path}`, detail: callstack });
} }
return r; return r;