Skip to content

Error Handling in TypeScript

Scattering error responses through every handler guarantees inconsistency. Instead, throw typed errors in your handlers and translate them to problem+json in one place.

flowchart LR
  H[Handler throws ApiError] --> M[Error middleware]
  M --> P[Map to problem+json]
  P --> R[Response with correct status]
Handlers throw; one place maps errors to responses

Define an error type that carries the status and problem fields, then a single handler that converts it. Anything unexpected becomes a 500 — and you never leak a stack trace to the client.

JavaScript
  • Consistency — every error leaves through the same shape.
  • Safety — unexpected errors become a generic 500; internals never leak.
  • Less code — handlers throw a line; they do not each build a response.
What is the main benefit of a central error handler?
How should an unexpected (unanticipated) error be returned to the client?
In this pattern, how do handlers signal failure?