[FIX] component: error handling in class inheritance

Before this commit, class inheritance when using the onError hook was unclear nay wrong.

After this commit, error handlers are called from the bottom up  in the inheritance hierarchy.
If a handler doesn't rethrow the error, the handling stops there and no other handler is called.
If a handler does rethrow, the handlers declared in a parent class are executed.
This commit is contained in:
Lucas Perais (lpe)
2021-12-01 15:42:22 +01:00
committed by Aaron Bohy
parent e91e50a812
commit c1a973a4d8
5 changed files with 164 additions and 9 deletions
+3 -2
View File
@@ -21,9 +21,10 @@ function _handleError(node: ComponentNode | null, error: any, isFirstRound = fal
}
let stopped = false;
for (const h of errorHandlers) {
// execute in the opposite order
for (let i = errorHandlers.length - 1; i >= 0; i--) {
try {
h(error);
errorHandlers[i](error);
stopped = true;
break;
} catch (e) {
+4 -5
View File
@@ -59,14 +59,13 @@ export function onRendered(fn: () => void | any) {
};
}
export function onError(fn: (error: Error) => void | any) {
type OnErrorCallback = (error: any) => void | any;
export function onError(callback: OnErrorCallback) {
const node = getCurrent()!;
let handlers = nodeErrorHandlers.get(node);
if (handlers) {
handlers.push(fn);
} else {
if (!handlers) {
handlers = [];
handlers.push(fn);
nodeErrorHandlers.set(node, handlers);
}
handlers.push(callback);
}