catch and report errors on handler

pull/8443/head
Fred Martini 4 years ago
parent ae57475f8b
commit 26b3cbe571

@ -88,9 +88,9 @@ export function createEventDispatcher<EventMap extends {} = any>(): <
// TODO are there situations where events could be dispatched // TODO are there situations where events could be dispatched
// in a server (non-DOM) environment? // in a server (non-DOM) environment?
const event = custom_event(type, detail, { cancelable }); const event = custom_event(type, detail, { cancelable });
callbacks.slice().forEach(fn => {
fn.call(component, event); invoke_callbacks(callbacks, component, event);
});
return !event.defaultPrevented; return !event.defaultPrevented;
} }
@ -151,6 +151,25 @@ export function bubble(component, event) {
if (callbacks) { if (callbacks) {
// @ts-ignore // @ts-ignore
callbacks.slice().forEach(fn => fn.call(this, event)); invoke_callbacks(callbacks, this, event);
}
}
// Invoke all callbacks, even if an error occur
function invoke_callbacks(callbacks, source, event) {
callbacks.slice().forEach(fn => {
try {
return fn.call(source, event);
} catch (err) {
// @ts-ignore
if (window.reportError) {
// @ts-ignore
window.reportError(err);
} else {
setTimeout(() => {
throw err;
}, 0);
}
} }
});
} }

Loading…
Cancel
Save