API reference / @backpack/error-handling / promises / catchErrors
Function: catchErrors()
ts
function catchErrors<TMethod, T>(errorCallback, ...errorCallbacks): (method) => PromiseMethod;
A decorator which can be applied on a method returning a Promise<T>
.
When the promise gets rejected, it will chain all given error callbacks onto the promise, enabling easy error recovery using one or more error converters.
Type Parameters
Type Parameter |
---|
TMethod extends PromiseMethod <unknown , any > |
T extends Promise <any > |
Parameters
Parameter | Type |
---|---|
errorCallback | (_ ) => T | Awaited <T > |
...errorCallbacks | (_ ) => T | Awaited <T >[] |
Returns
ts
(method): PromiseMethod;
Parameters
Parameter | Type |
---|---|
method | TMethod |
Returns
PromiseMethod
Example
ts
class MyService {
@catchErrors(
onFailure((error) => console.error("Got error", error)),
recoverIfInstanceOf(MyError, () => "fallback 1"),
orElse("fallback-2"),
)
public async mightFail(): Promise<string> {
// ...
return "foo";
}
}