[FIX] components: improve error handling

Owl provides a way to manage errors occuring in component lifecycle
methods. However, before this commit, these errors were not always
logged or visible, which is very annoying in the common developer
workflow (doing something, checking it works, seeing no error but a
broken interface).

In this commit, we make sure errors are logged/throws in all cases:

- if an error occurs in a mounting operation => the promise is rejected
(which will log the error)
- if an error occurs after the mounting operation and is not handled by
any error handlers => the error will be logged (with console.error).
Also, in that case, this commit adds a warning to explain that owl
destroys the root component, which will help developers understanding
what happened.
This commit is contained in:
Géry Debongnie
2021-12-15 15:55:13 +01:00
committed by Aaron Bohy
parent fd295b3be3
commit 983b9f996d
4 changed files with 107 additions and 78 deletions
+7 -1
View File
@@ -81,10 +81,12 @@ export class App<T extends typeof Component = any> extends TemplateSet {
mountNode(node: ComponentNode, target: HTMLElement, options?: MountOptions) {
const promise: any = new Promise((resolve, reject) => {
let isResolved = false;
// manually set a onMounted callback.
// that way, we are independant from the current node.
node.mounted.push(() => {
resolve(node.component);
isResolved = true;
});
// Manually add the last resort error handler on the node
@@ -94,7 +96,11 @@ export class App<T extends typeof Component = any> extends TemplateSet {
nodeErrorHandlers.set(node, handlers);
}
handlers.unshift((e) => {
reject(e);
if (isResolved) {
console.error(e);
} else {
reject(e);
}
throw e;
});
});
+4 -2
View File
@@ -57,9 +57,11 @@ export function handleError(params: ErrorParams) {
const handled = _handleError(node, error, true);
if (!handled) {
console.warn(`[Owl] Unhandled error. Destroying the root component`);
try {
node.app.destroy();
} catch (e) {}
} catch (e) {
console.error(e);
}
}
return handled;
}