From f0d2c191849ed0154c3ac57418a624fb997d4d71 Mon Sep 17 00:00:00 2001 From: Bruno Windels <274386+bwindels@users.noreply.github.com> Date: Mon, 9 Jan 2023 13:50:03 +0100 Subject: [PATCH] allow an explicit error value again in ErrorBoundary --- src/utils/ErrorBoundary.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/utils/ErrorBoundary.ts b/src/utils/ErrorBoundary.ts index 520d2a35..d13065f9 100644 --- a/src/utils/ErrorBoundary.ts +++ b/src/utils/ErrorBoundary.ts @@ -25,22 +25,22 @@ export class ErrorBoundary { * Executes callback() and then runs errorCallback() on error. * This will never throw but instead return `errorValue` if an error occured. */ - try(callback: () => T): T | typeof ErrorValue; - try(callback: () => Promise): Promise | typeof ErrorValue { + try(callback: () => T, errorValue?: E): T | typeof errorValue; + try(callback: () => Promise, errorValue?: E): Promise | typeof errorValue { try { - let result: T | Promise = callback(); + let result: T | Promise = callback(); if (result instanceof Promise) { result = result.catch(err => { this._error = err; this.errorCallback(err); - return ErrorValue; + return errorValue; }); } return result; } catch (err) { this._error = err; this.errorCallback(err); - return ErrorValue; + return errorValue; } } @@ -56,9 +56,9 @@ export function tests() { const boundary = new ErrorBoundary(() => emitted = true); const result = boundary.try(() => { throw new Error("fail!"); - }); + }, 0); assert(emitted); - assert.strictEqual(result, ErrorValue); + assert.strictEqual(result, 0); }, "return value of callback is forwarded": assert => { let emitted = false; @@ -74,9 +74,9 @@ export function tests() { const boundary = new ErrorBoundary(() => emitted = true); const result = await boundary.try(async () => { throw new Error("fail!"); - }); + }, 0); assert(emitted); - assert.strictEqual(result, ErrorValue); + assert.strictEqual(result, 0); } } } \ No newline at end of file