function errorHandler
Since 0.1.0
errorHandler(options?: ErrorHandlerOptions): MiddlewareFunction

Creates a global error-handler middleware.

Behavior:

  1. Calls next() inside a try/catch.
  2. If next() throws an HttpError, its statusCode is used as-is.
  3. If next() throws any other Error, it is wrapped in a 500 internalServerError carrying the original as cause.
  4. When configured, lets the application write its own response. A returned or resolved HandlerResult short-circuits formatting; undefined falls through.
  5. When logErrors is on and a logger is registered, the unmasked error is logged with the status the selected response serves.
  6. Otherwise, formats the error via selectFormatter, optionally enriches it with a stack trace, and sends it with the right status and content type. The middleware returns a HandlerResult (short-circuit) and never re-invokes next().

Parameters

optional
options: ErrorHandlerOptions

Error handler configuration

Return Type

MiddlewareFunction

A middleware function

Usage

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