mirror of
https://github.com/vector-im/hydrogen-web.git
synced 2024-12-23 11:35:04 +01:00
allow an explicit error value again in ErrorBoundary
This commit is contained in:
parent
999a3ca5d2
commit
f0d2c19184
@ -25,22 +25,22 @@ export class ErrorBoundary {
|
|||||||
* Executes callback() and then runs errorCallback() on error.
|
* Executes callback() and then runs errorCallback() on error.
|
||||||
* This will never throw but instead return `errorValue` if an error occured.
|
* This will never throw but instead return `errorValue` if an error occured.
|
||||||
*/
|
*/
|
||||||
try<T>(callback: () => T): T | typeof ErrorValue;
|
try<T, E>(callback: () => T, errorValue?: E): T | typeof errorValue;
|
||||||
try<T>(callback: () => Promise<T>): Promise<T | typeof ErrorValue> | typeof ErrorValue {
|
try<T, E>(callback: () => Promise<T>, errorValue?: E): Promise<T | typeof errorValue> | typeof errorValue {
|
||||||
try {
|
try {
|
||||||
let result: T | Promise<T | typeof ErrorValue> = callback();
|
let result: T | Promise<T | typeof errorValue> = callback();
|
||||||
if (result instanceof Promise) {
|
if (result instanceof Promise) {
|
||||||
result = result.catch(err => {
|
result = result.catch(err => {
|
||||||
this._error = err;
|
this._error = err;
|
||||||
this.errorCallback(err);
|
this.errorCallback(err);
|
||||||
return ErrorValue;
|
return errorValue;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this._error = err;
|
this._error = err;
|
||||||
this.errorCallback(err);
|
this.errorCallback(err);
|
||||||
return ErrorValue;
|
return errorValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,9 +56,9 @@ export function tests() {
|
|||||||
const boundary = new ErrorBoundary(() => emitted = true);
|
const boundary = new ErrorBoundary(() => emitted = true);
|
||||||
const result = boundary.try(() => {
|
const result = boundary.try(() => {
|
||||||
throw new Error("fail!");
|
throw new Error("fail!");
|
||||||
});
|
}, 0);
|
||||||
assert(emitted);
|
assert(emitted);
|
||||||
assert.strictEqual(result, ErrorValue);
|
assert.strictEqual(result, 0);
|
||||||
},
|
},
|
||||||
"return value of callback is forwarded": assert => {
|
"return value of callback is forwarded": assert => {
|
||||||
let emitted = false;
|
let emitted = false;
|
||||||
@ -74,9 +74,9 @@ export function tests() {
|
|||||||
const boundary = new ErrorBoundary(() => emitted = true);
|
const boundary = new ErrorBoundary(() => emitted = true);
|
||||||
const result = await boundary.try(async () => {
|
const result = await boundary.try(async () => {
|
||||||
throw new Error("fail!");
|
throw new Error("fail!");
|
||||||
});
|
}, 0);
|
||||||
assert(emitted);
|
assert(emitted);
|
||||||
assert.strictEqual(result, ErrorValue);
|
assert.strictEqual(result, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user