errorHandler(options?: ErrorHandlerOptions): MiddlewareFunction
Creates a global error-handler middleware.
Behavior:
- Calls
next()inside a try/catch. - If
next()throws anHttpError, itsstatusCodeis used as-is. - If
next()throws any otherError, it is wrapped in a500internalServerErrorcarrying the original ascause. - When configured, lets the application write its own response. A returned
or resolved
HandlerResultshort-circuits formatting;undefinedfalls through. - When
logErrorsis on and a logger is registered, the unmasked error is logged with the status the selected response serves. - Otherwise, formats the error via
selectFormatter, optionally enriches it with astacktrace, and sends it with the right status and content type. The middleware returns aHandlerResult(short-circuit) and never re-invokesnext().
optional
options: ErrorHandlerOptions
Error handler configuration