-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy patherrors.ts
More file actions
54 lines (49 loc) · 1.72 KB
/
errors.ts
File metadata and controls
54 lines (49 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { getErrorMessage, getErrorStack } from "../../pure/helpers-pure";
import { vscode } from "../vscode-api";
// Keep track of previous errors that have happened.
// The listeners for uncaught errors and rejections can get triggered
// twice for each error. This is believed to be an effect caused
// by React's error boundaries. Adding an error boundary stops
// this duplicate reporting for errors that happen during component
// rendering, but unfortunately errors from event handlers and
// timeouts are still duplicated and there does not appear to be
// a way around this.
const previousErrors: Set<Error> = new Set();
function shouldReportError(error: Error): boolean {
const seenBefore = previousErrors.has(error);
previousErrors.add(error);
setTimeout(() => {
previousErrors.delete(error);
}, 1000);
return !seenBefore;
}
const unhandledErrorListener = (event: ErrorEvent) => {
if (shouldReportError(event.error)) {
vscode.postMessage({
t: "unhandledError",
error: {
message: getErrorMessage(event.error),
stack: getErrorStack(event.error),
},
});
}
};
const unhandledRejectionListener = (event: PromiseRejectionEvent) => {
if (shouldReportError(event.reason)) {
vscode.postMessage({
t: "unhandledError",
error: {
message: getErrorMessage(event.reason),
stack: getErrorStack(event.reason),
},
});
}
};
/**
* Adds listeners for unhandled errors / rejected promises.
* When an error is detected a "unhandledError" message is posted to the view.
*/
export function registerUnhandledErrorListener() {
window.addEventListener("error", unhandledErrorListener);
window.addEventListener("unhandledrejection", unhandledRejectionListener);
}