Skip to content
1 min read · 190 words

Function: createException()

ts
function createException<T>(
  name: string,
  message: string,
  code: string,
  status?: number,
  fatal?: boolean,
): CreatedException<T>;

Factory that produces a named @nhtio/adk!BaseException subclass with a fixed printf-style message template, error code, HTTP status, and fatality flag.

Type Parameters

Type ParameterDefault typeDescription
T extends any[][]Tuple of printf format argument types. Pass a non-empty tuple to require callers to supply interpolation values at the throw site.

Parameters

ParameterTypeDescription
namestringThe name property set on thrown instances (used by isNamedException).
messagestringPrintf-style template string for the error message.
codestringMachine-readable error code stored on the static and instance code property.
status?numberHTTP status code associated with this exception class.
fatal?booleanWhen true, signals that the error is unrecoverable.

Returns

CreatedException<T>

A constructor for a @nhtio/adk!BaseException subclass with the given metadata baked in.

Remarks

Prefer this over hand-writing subclasses for simple, static exception definitions.

Example

ts
export const E_NOT_FOUND = createException<[string]>(
  "E_NOT_FOUND",
  "Resource %s not found",
  "E_NOT_FOUND",
  404,
  false,
);
throw new E_NOT_FOUND(["my-id"]);