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