Skip to content

Latest commit

 

History

History
51 lines (37 loc) · 1.16 KB

File metadata and controls

51 lines (37 loc) · 1.16 KB

onError

Handle thrown errors and promise rejections in one place. The handler can return a fallback value, start recovery work, or rethrow.

Import

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.

Signature

onError<This, Return, Args>({
	func: keyof This | ((error: unknown, args: Args) => Return | Promise<Return>),
})

Example

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`);
	}
}

Notes

  • onError is a method decorator.
  • The handler receives the error and the original arguments.
  • It handles both synchronous throws and async rejections.

Related