From daad19c0604305ed00e40cb06be42a987e3d56c8 Mon Sep 17 00:00:00 2001 From: Bruno Windels <274386+bwindels@users.noreply.github.com> Date: Thu, 19 Jan 2023 11:37:39 +0100 Subject: [PATCH] swallow errors in errorCallback in ErrorBoundary nothing should be able to make ErrorBoundary.try throw --- src/utils/ErrorBoundary.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/utils/ErrorBoundary.ts b/src/utils/ErrorBoundary.ts index a4ac459c..a74da479 100644 --- a/src/utils/ErrorBoundary.ts +++ b/src/utils/ErrorBoundary.ts @@ -32,18 +32,26 @@ export class ErrorBoundary { if (result instanceof Promise) { result = result.catch(err => { this._error = err; - this.errorCallback(err); + this.reportError(err); return errorValue; }); } return result; } catch (err) { this._error = err; - this.errorCallback(err); + this.reportError(err); return errorValue; } } + private reportError(err: Error) { + try { + this.errorCallback(err); + } catch (err) { + console.error("error in ErrorBoundary callback", err); + } + } + get error(): Error | undefined { return this._error; } @@ -77,6 +85,15 @@ export function tests() { }, 0); assert(emitted); assert.strictEqual(result, 0); + }, + "exception in error callback is swallowed": async assert => { + let emitted = false; + const boundary = new ErrorBoundary(() => { throw new Error("bug in errorCallback"); }); + assert.doesNotThrow(() => { + boundary.try(() => { + throw new Error("fail!"); + }); + }); } } }