Handle thrown errors and promise rejections in one place. The handler can return a fallback value, start recovery work, or rethrow.
import { onError } from "decorator-toolkit/on-error";For legacy TypeScript decorators, import from decorator-toolkit/on-error/legacy or import { onError } from decorator-toolkit/legacy.
onError<This, Return, Args>({
func: keyof This | ((error: unknown, args: Args) => Return | Promise<Return>),
})import { onError } from "decorator-toolkit/on-error";
class ProfileLoader {
recover(_error: unknown, args: [string]): Promise<{ id: string; cached: true; }> {
return Promise.resolve({ id: args[0], cached: true });
}
@onError<ProfileLoader, { id: string; cached: true; }, [string]>({
func: "recover",
})
async load(id: string): Promise<{ id: string; cached: true; }> {
throw new Error(`profile:${id}:failed`);
}
}onErroris a method decorator.- The handler receives the error and the original arguments.
- It handles both synchronous throws and async rejections.