mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[IMP] component: improve errors when thrown from lifecycle hooks
Previously, a crash in a lifecycle hook for any reason would throw an error whose stack trace started from the scheduler and contained only the place where the hook was called by owl, but not the place where the hook was registered by the user. This proved very difficult for users to debug as they cannot really tell which component registered that hook. This commit alleviates the issue by creating a new Error when the hook is originally called, and wrapping the registered callback in a try catch, throwing an error with the correct stack trace instead of the error in the hook, and setting the error in the hook as the cause of this synthetic error.
This commit is contained in:
committed by
Géry Debongnie
parent
4a922ed82d
commit
804ad3c35e
@@ -1,62 +1,97 @@
|
||||
import { getCurrent } from "./component_node";
|
||||
import { nodeErrorHandlers } from "./error_handling";
|
||||
|
||||
function wrapError(fn: (...args: any[]) => any, hookName: string) {
|
||||
const error = new Error(`The following error occurred in ${hookName}: `) as Error & {
|
||||
cause: any;
|
||||
};
|
||||
return (...args: any[]) => {
|
||||
try {
|
||||
const result = fn(...args);
|
||||
if (result instanceof Promise) {
|
||||
return result.catch((cause) => {
|
||||
error.cause = cause;
|
||||
if (cause instanceof Error) {
|
||||
error.message += `"${cause.message}"`;
|
||||
}
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
return result;
|
||||
} catch (cause) {
|
||||
if (cause instanceof Error) {
|
||||
error.message += `"${cause.message}"`;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// hooks
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
export function onWillStart(fn: () => Promise<void> | void | any) {
|
||||
const node = getCurrent();
|
||||
node.willStart.push(fn.bind(node.component));
|
||||
const decorate = node.app.dev ? wrapError : (fn: any) => fn;
|
||||
node.willStart.push(decorate(fn.bind(node.component), "onWillStart"));
|
||||
}
|
||||
|
||||
export function onWillUpdateProps(fn: (nextProps: any) => Promise<void> | void | any) {
|
||||
const node = getCurrent();
|
||||
node.willUpdateProps.push(fn.bind(node.component));
|
||||
const decorate = node.app.dev ? wrapError : (fn: any) => fn;
|
||||
node.willUpdateProps.push(decorate(fn.bind(node.component), "onWillUpdateProps"));
|
||||
}
|
||||
|
||||
export function onMounted(fn: () => void | any) {
|
||||
const node = getCurrent();
|
||||
node.mounted.push(fn.bind(node.component));
|
||||
const decorate = node.app.dev ? wrapError : (fn: any) => fn;
|
||||
node.mounted.push(decorate(fn.bind(node.component), "onMounted"));
|
||||
}
|
||||
|
||||
export function onWillPatch(fn: () => Promise<void> | any | void) {
|
||||
const node = getCurrent();
|
||||
node.willPatch.unshift(fn.bind(node.component));
|
||||
const decorate = node.app.dev ? wrapError : (fn: any) => fn;
|
||||
node.willPatch.unshift(decorate(fn.bind(node.component), "onWillPatch"));
|
||||
}
|
||||
|
||||
export function onPatched(fn: () => void | any) {
|
||||
const node = getCurrent();
|
||||
node.patched.push(fn.bind(node.component));
|
||||
const decorate = node.app.dev ? wrapError : (fn: any) => fn;
|
||||
node.patched.push(decorate(fn.bind(node.component), "onPatched"));
|
||||
}
|
||||
|
||||
export function onWillUnmount(fn: () => Promise<void> | void | any) {
|
||||
const node = getCurrent();
|
||||
node.willUnmount.unshift(fn.bind(node.component));
|
||||
const decorate = node.app.dev ? wrapError : (fn: any) => fn;
|
||||
node.willUnmount.unshift(decorate(fn.bind(node.component), "onWillUnmount"));
|
||||
}
|
||||
|
||||
export function onWillDestroy(fn: () => Promise<void> | void | any) {
|
||||
const node = getCurrent();
|
||||
node.willDestroy.push(fn.bind(node.component));
|
||||
const decorate = node.app.dev ? wrapError : (fn: any) => fn;
|
||||
node.willDestroy.push(decorate(fn.bind(node.component), "onWillDestroy"));
|
||||
}
|
||||
|
||||
export function onWillRender(fn: () => void | any) {
|
||||
const node = getCurrent();
|
||||
const renderFn = node.renderFn;
|
||||
node.renderFn = () => {
|
||||
const decorate = node.app.dev ? wrapError : (fn: any) => fn;
|
||||
node.renderFn = decorate(() => {
|
||||
fn.call(node.component);
|
||||
return renderFn();
|
||||
};
|
||||
}, "onWillRender");
|
||||
}
|
||||
|
||||
export function onRendered(fn: () => void | any) {
|
||||
const node = getCurrent();
|
||||
const renderFn = node.renderFn;
|
||||
node.renderFn = () => {
|
||||
const decorate = node.app.dev ? wrapError : (fn: any) => fn;
|
||||
node.renderFn = decorate(() => {
|
||||
const result = renderFn();
|
||||
fn.call(node.component);
|
||||
return result;
|
||||
};
|
||||
}, "onRendered");
|
||||
}
|
||||
|
||||
type OnErrorCallback = (error: any) => void | any;
|
||||
|
||||
Reference in New Issue
Block a user