import { STATUS_CODES } from 'node:http'; import { CustomError } from '@celljs/core'; export interface HttpErrorOptions { /** The error behind this one; logged with it, never sent. */ cause?: unknown; /** Whether the client gets `message`; default: for 4xx, not for 5xx (see `web.errors.exposeMessages`). */ expose?: boolean; } /** An error that answers with `statusCode`. Without a message, the message is the status text (`Not Found`). */ export class HttpError extends CustomError { readonly expose: boolean; constructor(public statusCode: number, message?: string, options: HttpErrorOptions = {}) { super(message ?? STATUS_CODES[statusCode] ?? `HTTP ${statusCode}`, options.cause === undefined ? undefined : { cause: options.cause }); this.expose = options.expose ?? statusCode < 500; } }