Skip to content

Commit 3fe0699

Browse files
Avoid reporting errors twice
1 parent 969fdb6 commit 3fe0699

1 file changed

Lines changed: 37 additions & 14 deletions

File tree

  • extensions/ql-vscode/src/view/common

extensions/ql-vscode/src/view/common/errors.ts

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,47 @@
11
import { getErrorMessage, getErrorStack } from "../../pure/helpers-pure";
22
import { vscode } from "../vscode-api";
33

4+
// Keep track of previous errors that have happened.
5+
// The listeners for uncaught errors and rejections can get triggered
6+
// twice for each error. This is believed to be an effect caused
7+
// by React's error boundaries. Adding an error boundary stops
8+
// this duplicate reporting for errors that happen during component
9+
// rendering, but unfortunately errors from event handlers and
10+
// timeouts are still duplicated and there does not appear to be
11+
// a way around this.
12+
const previousErrors: Set<Error> = new Set();
13+
14+
function shouldReportError(error: Error): boolean {
15+
const seenBefore = previousErrors.has(error);
16+
previousErrors.add(error);
17+
setTimeout(() => {
18+
previousErrors.delete(error);
19+
}, 1000);
20+
return !seenBefore;
21+
}
22+
423
const unhandledErrorListener = (event: ErrorEvent) => {
5-
vscode.postMessage({
6-
t: "unhandledError",
7-
error: {
8-
message: getErrorMessage(event.error),
9-
stack: getErrorStack(event.error),
10-
},
11-
});
24+
if (shouldReportError(event.error)) {
25+
vscode.postMessage({
26+
t: "unhandledError",
27+
error: {
28+
message: getErrorMessage(event.error),
29+
stack: getErrorStack(event.error),
30+
},
31+
});
32+
}
1233
};
1334

1435
const unhandledRejectionListener = (event: PromiseRejectionEvent) => {
15-
vscode.postMessage({
16-
t: "unhandledError",
17-
error: {
18-
message: getErrorMessage(event.reason),
19-
stack: getErrorStack(event.reason),
20-
},
21-
});
36+
if (shouldReportError(event.reason)) {
37+
vscode.postMessage({
38+
t: "unhandledError",
39+
error: {
40+
message: getErrorMessage(event.reason),
41+
stack: getErrorStack(event.reason),
42+
},
43+
});
44+
}
2245
};
2346

2447
/**

0 commit comments

Comments
 (0)