fix(core): global error handler should be registered outside webpack runtime (#8556)

This commit is contained in:
forehalo
2024-10-21 05:58:05 +00:00
parent db374f7feb
commit 1ed9775c45
7 changed files with 99 additions and 92 deletions

View File

@@ -1,5 +1,4 @@
// ORDER MATTERS
import './global-error-handler';
import './env';
import './public-path';
import './polyfill/browser';

Binary file not shown.

Before

Width:  |  Height:  |  Size: 37 KiB

View File

@@ -1,81 +0,0 @@
/* eslint-disable no-var */
import errorImg from './error.png';
var errorEl: HTMLDivElement | null = null;
function showGlobalErrorPage() {
if (errorEl) {
return;
}
errorEl = document.createElement('div');
errorEl.innerHTML = [
'<style>',
'.gue {display:flex;flex-direction:column;align-items:center;justify-content:center;width:380px;}',
'.gue img{width:380px;}',
'.gue div{padding:16px 40px 0 40px;text-align:center;}',
'.gue .p1{color:#141414;line-height:24px;font-weight:500;}',
'.gue .p2{color:#7A7A7A;line-height:22px;}',
'</style>',
'<div class="gue">',
'<img src="',
errorImg,
'" />',
'<div>',
'<p class="p1">Unsupported Environment</p>',
'<p class="p2">',
'It looks like AFFiNE cannot run in this environment.',
"Please ensure you are using a supported browser or update your device's operating system to the latest version.",
'If the issue persists, visit our <a href="https://github.com/toeverything/AFFiNE/issues">support page</a> for further assistance.',
'</p>',
'</div>',
'</div>',
].join('');
errorEl.setAttribute(
'style',
'position:absolute;top:0;left:0;height:100vh;width:100vw;display:flex;flex-direction:column;align-items:center;justify-content:center;background:white;z-index:999;'
);
document.body.append(errorEl);
}
function registerGlobalErrorHandler() {
function handleGlobalUnrecoverableError(
e: ErrorEvent | PromiseRejectionEvent
) {
var error =
'error' in e ? e.error : e.reason instanceof Error ? e.reason : null;
console.error('unhandled unrecoverable error', error);
const shouldCache =
// syntax error
error && error instanceof SyntaxError;
if (!shouldCache) {
return;
}
e.stopImmediatePropagation();
showGlobalErrorPage();
}
if (typeof document !== 'undefined') {
globalThis.addEventListener(
'unhandledrejection',
handleGlobalUnrecoverableError
);
globalThis.addEventListener('error', handleGlobalUnrecoverableError);
}
}
function ensureBasicEnvironment() {
var globals = ['Promise', 'Map', 'fetch', 'customElements'];
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (var i = 0; i < globals.length; i++) {
if (!(globals[i] in globalThis)) {
showGlobalErrorPage();
return;
}
}
}
registerGlobalErrorHandler();
ensureBasicEnvironment();