Skip to content

Commit

Permalink
fix(core): handle global script error (#8576)
Browse files Browse the repository at this point in the history
  • Loading branch information
forehalo committed Oct 22, 2024
1 parent be3125b commit ff95f12
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions tools/cli/src/webpack/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,37 @@
document.body.append(errorEl);
}

function registerGlobalErrorHandler() {
function handler(e) {
var error =
'error' in e ? e.error : e.reason instanceof Error ? e.reason : null;
console.error('unhandled unrecoverable error', error);
/**
* @param event {PromiseRejectionEvent|ErrorEvent}
*/
function handler(event) {
var error;

const shouldCache =
// syntax error
error && error instanceof SyntaxError;
if ('error' in event) {
error =
event.error ||
(event.message === 'Script error.'
? new SyntaxError(event.message)
: new Error(event.message));
} else {
error = event.reason;
}

if (!shouldCache) {
return;
}
console.error('unhandled unrecoverable error', error);

var shouldCache =
// syntax error
error && error instanceof SyntaxError;

e.stopImmediatePropagation();
showGlobalErrorPage();
if (!shouldCache) {
return;
}

event.stopImmediatePropagation();
showGlobalErrorPage();
}

function registerGlobalErrorHandler() {
if (typeof document !== 'undefined') {
globalThis.addEventListener('unhandledrejection', handler);
globalThis.addEventListener('error', handler);
Expand Down

0 comments on commit ff95f12

Please sign in to comment.