class HttpError
extends Error
Since 0.1.0

The framework's HTTP error type.

Throw an instance directly, or — preferably — construct one via a factory function from ./exceptions.ts so the status code is correct by construction:

Examples

Example 1

import { notFound } from '@setu-ts/exceptions';

throw notFound(`User ${id} not found`);

The error handler middleware inspects the statusCode to set the response status, and serializes message and details into the error body.

Constructors

HttpError(
statusCode: number,
message: string,
details?: Readonly<Record<string, unknown>>,
cause?: Error
)

Creates a new HttpError.

Prefer the factory functions in exceptions.ts over calling this constructor directly — they guarantee a correct status code.

Parameters
statusCode: number

HTTP status code (e.g. 404)

message: string

Human-readable error message

optional
details: Readonly<Record<string, unknown>>

Optional structured details; omitted when absent

optional
cause: Error

Optional underlying error; forwarded to the ES2022 cause chain

Example 1

const err = new HttpError(404, 'User not found');
console.log(err.statusCode); // 404

Static Methods

Since 0.1.0
from(init: HttpErrorInit): HttpError

Creates an HttpError from an HttpErrorInit object.

Convenience overload used by factory functions so they can branch on optional details/cause once.

Properties

readonly
optional
details: Readonly<Record<string, unknown>>

Structured details appended to the error body. Omitted entirely when not supplied (never undefined) so serialization stays clean.

declare suppresses the ES2022 class-field initialization that would otherwise set this to undefined on every instance — it only exists on the object when actually assigned by the constructor.

readonly
statusCode: number

The HTTP status code this error maps to.

Usage

import { HttpError } from "exceptions/src/index.ts";