|
1 | 1 | import { getErrorMessage, getErrorStack } from "../../pure/helpers-pure"; |
2 | 2 | import { vscode } from "../vscode-api"; |
3 | 3 |
|
| 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 | + |
4 | 23 | 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 | + } |
12 | 33 | }; |
13 | 34 |
|
14 | 35 | 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 | + } |
22 | 45 | }; |
23 | 46 |
|
24 | 47 | /** |
|
0 commit comments