Repository navigation
Expand file tree
/
Copy pathhandleError.ts
More file actions
67 lines (57 loc) · 1.61 KB
/
Copy pathhandleError.ts
File metadata and controls
67 lines (57 loc) · 1.61 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
55
56
57
58
59
60
61
62
63
64
65
66
67
import { DEBUG } from '../config';
import { isCurrentTabMaster } from './establishMultitabRole';
import { throttle } from './schedulers';
const DEBUG_ALERT_MSG = 'Shoot!\nSomething went wrong, please see the error details in Dev Tools Console.';
let showError = true;
let error: Error | undefined;
window.addEventListener('error', handleErrorEvent);
window.addEventListener('unhandledrejection', handleErrorEvent);
if (DEBUG) {
window.addEventListener('focus', () => {
if (!isCurrentTabMaster()) {
return;
}
showError = true;
if (error) {
window.alert(getErrorMessage(error));
error = undefined;
}
});
window.addEventListener('blur', () => {
if (!isCurrentTabMaster()) {
return;
}
showError = false;
});
}
const throttleError = throttle((err) => {
if (showError) {
window.alert(getErrorMessage(err));
} else {
error = err;
}
}, 1500);
export function handleError(err: Error) {
// eslint-disable-next-line no-console
console.error(err);
if (DEBUG) {
throttleError(err);
}
}
function handleErrorEvent(e: ErrorEvent | PromiseRejectionEvent) {
if (e instanceof ErrorEvent) {
// https://stackoverflow.com/questions/49384120/resizeobserver-loop-limit-exceeded
if (e.message === 'ResizeObserver loop limit exceeded') {
return;
}
// Flood wait errors
if (e.message.includes('A wait of')) {
return;
}
}
e.preventDefault();
handleError(e instanceof ErrorEvent ? (e.error || e.message) : e.reason);
}
function getErrorMessage(err: Error) {
return `${DEBUG_ALERT_MSG}\n\n${(err?.message) || err}\n${err?.stack}`;
}