function withHttpStatusHint
Since 0.4.0
withHttpStatusHint<T extends Error>(
error: T,
): T

Brands an error with the status it should be answered with.

The error is branded in place and returned, so identity is preserved and a throw withHttpStatusHint(new Xyz(…), hint) reads as one statement. The property is symbol-keyed and non-enumerable, so it is invisible to Object.keys, JSON.stringify and spread, and the error behaves exactly as it did — instanceof, name, message and cause are all untouched.

Examples

Example 1

export class UnsupportedThingError extends Error {
  constructor(thing: string, message: string) {
    super(message);
    withHttpStatusHint(this, {
      status: 501,
      title: 'Not Implemented',
      detail: `'${thing}' is not supported.`,
    });
  }
}

Type Parameters

T extends Error

The error type, preserved in the return

Parameters

error: T

The error to brand

How it should be answered

Return Type

The same error reference, branded

Throws

TypeError

If error is frozen, sealed, or otherwise not extensible — the brand is defined as a property on the error itself

Usage

import { withHttpStatusHint } from "common/src/index.ts";