TelegramPWA/src/util/handleError.ts

39 lines
1.2 KiB
TypeScript

import { DEBUG_ALERT_MSG } from '../config';
import { getAllMultitabTokens } from './establishMultitabRole';
import { throttle } from './schedulers';
import { IS_MULTITAB_SUPPORTED } from './windowEnvironment';
// eslint-disable-next-line prefer-destructuring
const APP_ENV = process.env.APP_ENV;
window.addEventListener('error', handleErrorEvent);
window.addEventListener('unhandledrejection', handleErrorEvent);
function handleErrorEvent(e: ErrorEvent | PromiseRejectionEvent) {
// https://stackoverflow.com/questions/49384120/resizeobserver-loop-limit-exceeded
if (e instanceof ErrorEvent && e.message === 'ResizeObserver loop limit exceeded') {
return;
}
e.preventDefault();
handleError(e instanceof ErrorEvent ? (e.error || e.message) : e.reason);
}
const throttledAlert = throttle((message: string) => {
if (IS_MULTITAB_SUPPORTED && getAllMultitabTokens().length > 1) {
return;
}
// eslint-disable-next-line no-alert
window.alert(message);
}, 1000);
export function handleError(err: Error) {
// eslint-disable-next-line no-console
console.error(err);
if (APP_ENV === 'development' || APP_ENV === 'staging') {
throttledAlert(`${DEBUG_ALERT_MSG}\n\n${(err?.message) || err}\n${err?.stack}`);
}
}