From 2c563ee380e467487df2420232264b2b48d022ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Sat, 26 Oct 2019 10:27:56 +0200 Subject: [PATCH] [REF] component: move error handling into the fiber --- src/component/component.ts | 45 +++++--------------------------------- src/component/fiber.ts | 29 +++++++++++++++++++++++- 2 files changed, 34 insertions(+), 40 deletions(-) diff --git a/src/component/component.ts b/src/component/component.ts index 99e5a2e8..b1ef1462 100644 --- a/src/component/component.ts +++ b/src/component/component.ts @@ -262,8 +262,11 @@ export class Component { /** * catchError is a method called whenever some error happens in the rendering or * lifecycle hooks of a child. + * + * It needs to be implemented by a component that is designed to handle the + * error properly. */ - catchError(error: Error): void {} + catchError?(error?: Error):void; //-------------------------------------------------------------------------- // Public @@ -561,7 +564,7 @@ export class Component { try { await Promise.all([this.willStart(), this.__owl__.willStartCB && this.__owl__.willStartCB()]); } catch (e) { - errorHandler(e, fiber); + fiber.handleError(e); fiber.vnode = h("div"); // -> we render this div at the end return Promise.resolve(); } @@ -586,7 +589,7 @@ export class Component { }); } catch (e) { vnode = __owl__.vnode || h("div"); - errorHandler(e, fiber); + fiber.handleError(e); } fiber.vnode = vnode; if (__owl__.observer) { @@ -650,39 +653,3 @@ export class Component { return props; } } - -//------------------------------------------------------------------------------ -// Error handling -//------------------------------------------------------------------------------ - -Fiber.prototype.handleError = function(error) { - errorHandler(error, this); -}; -/** - * This is the global error handler for errors occurring in Owl main lifecycle - * methods. Caught errors are triggered on the QWeb instance, and are - * potentially given to some parent component which implements `catchError`. - * - * If there are no such component, we destroy everything. This is better than - * being in a corrupted state. - */ -function errorHandler(error: Error, fiber: Fiber) { - let canCatch = false; - let component = fiber.component; - let qweb = component.env.qweb; - let root = component; - while (component && !(canCatch = component.catchError !== Component.prototype.catchError)) { - root = component; - component = component.__owl__.parent!; - } - console.error(error); - qweb.trigger("error", error); - - if (canCatch) { - setTimeout(() => { - component.catchError(error); - }); - } else { - root.destroy(); - } -} diff --git a/src/component/fiber.ts b/src/component/fiber.ts index 157e3b00..182c5dee 100644 --- a/src/component/fiber.ts +++ b/src/component/fiber.ts @@ -195,5 +195,32 @@ export class Fiber { }); } - handleError(e: Error) {} + /** + * This is the global error handler for errors occurring in Owl main lifecycle + * methods. Caught errors are triggered on the QWeb instance, and are + * potentially given to some parent component which implements `catchError`. + * + * If there are no such component, we destroy everything. This is better than + * being in a corrupted state. + */ + handleError(error: Error) { + let canCatch = false; + let component = this.component; + let qweb = component.env.qweb; + let root = component; + while (component && !(canCatch = !!component.catchError)) { + root = component; + component = component.__owl__.parent!; + } + console.error(error); + qweb.trigger("error", error); + + if (canCatch) { + setTimeout(() => { + component.catchError!(error); + }); + } else { + root.destroy(); + } + } }