mirror of
https://github.com/priyanshujain/sanderling.git
synced 2026-10-05 04:27:09 +00:00
feat(web-runtime): capture uncaught errors into state.exceptions
This commit is contained in:
1 parent
b4ab121fcb
commit
f26ede7151
1 file changed
+31
-1
@@ -316,6 +316,36 @@ function buildAx(): unknown {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Uncaught errors and unhandled rejections are buffered here as they fire, so
|
||||||
|
// the default noUncaughtExceptions property can observe them. Without this the
|
||||||
|
// web state.exceptions would always be empty and a page that throws would
|
||||||
|
// silently pass.
|
||||||
|
interface CapturedException {
|
||||||
|
class: string;
|
||||||
|
message: string;
|
||||||
|
stackTrace: string;
|
||||||
|
unixMillis: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const capturedExceptions: CapturedException[] = [];
|
||||||
|
|
||||||
|
function recordException(error: unknown): void {
|
||||||
|
const asError = error instanceof Error ? error : undefined;
|
||||||
|
capturedExceptions.push({
|
||||||
|
class: asError?.name ?? "Error",
|
||||||
|
message: asError?.message ?? String(error),
|
||||||
|
stackTrace: asError?.stack ?? "",
|
||||||
|
unixMillis: Date.now(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
globalThis.addEventListener("error", (event: ErrorEvent) => {
|
||||||
|
recordException(event.error ?? event.message);
|
||||||
|
});
|
||||||
|
globalThis.addEventListener("unhandledrejection", (event: PromiseRejectionEvent) => {
|
||||||
|
recordException(event.reason);
|
||||||
|
});
|
||||||
|
|
||||||
function buildState(): unknown {
|
function buildState(): unknown {
|
||||||
return {
|
return {
|
||||||
snapshots: {},
|
snapshots: {},
|
||||||
@@ -325,7 +355,7 @@ function buildState(): unknown {
|
|||||||
lastAction: null,
|
lastAction: null,
|
||||||
time: 0,
|
time: 0,
|
||||||
logs: [],
|
logs: [],
|
||||||
exceptions: [],
|
exceptions: capturedExceptions.slice(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user