published fresh until

Express: route async failures to error middleware

Summary On Express 5 return or await the handler promise; on Express 4 attach one rejection forwarding path, then register the final four-argument handler with app.use()

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: register a final app.use((err, req, res, next) => ...) error handler, then select promise forwarding for the installed Express major.

Short answer

As of November 2026, Express 5.3.0 automatically forwards failures from returned handler promises. The v4 guide says: errors from rejected promises are not passed to next automatically. The v5 migration guide defines the boundary; an old asynchronous-handler answer must not silently determine current behavior.

Choose / don't choose

  • Express 5: use async handlers with awaited operations, or return the promise chain. Timer callbacks still require their own error path (current guide).
  • Express 4: use one promise wrapper ending in .catch(next); don't both call next(err) and rethrow the same failure (v4 guide).
  • Background tasks: own failures outside the response lifecycle; don't route a later task failure into a response that is already complete.

Not for: framework selection; see hono-or-fastify-or-express-for-a-node-api-2026.

Settings that matter

Setting or flagValue / defaultApplies toSource
app.use error callbackExactly four arguments (err, req, res, next); register after routesExpress current middleware contractFAQ
.catch(next)Single terminal rejection handler on the wrapper's promise chainThe v4 guide: rejected promises are not passed to next automaticallyv4 guide
Returned handler promiseReturn/await it; rejection forwarding is automaticExpress 5migration
res.headersSentIf true, return next(err); do not send another responseExpress current error handlingguide
app.use fallbackMiddleware at the very bottom of the stack to handle a 404 response, before error handlersCurrent Express routingFAQ

Order of operations

1. Register parsers, authentication and routes before a normal unmatched-route fallback; put four-argument error middleware last. A 404 is not automatically an error (FAQ). 2. On Express 4, an application wrapper adapting the documented promise chain is:

const wrap = handler => (req, res, next) =>
  Promise.resolve().then(() => handler(req, res, next)).catch(next);

The handler must either throw/reject or forward the error, once. (source) 3. On Express 5, return/await the operation directly. No compatibility wrapper is required for that returned rejection (migration). 4. At the final error boundary, delegate if headers are already sent; otherwise send one controlled error response. Do not expose internal stacks. A late streaming failure may require closing the connection (guide). 5. Exercise a synchronous throw, rejected awaited operation, unmatched route, timer callback failure and post-header failure. Confirm each follows its intended path once.

Known failures and fixes

Error (short quote)CauseFixSource
ERR_HTTP_HEADERS_SENT / late response failureSending another response after headers beganDelegate through res.headersSent; don't attempt a second responseNode error, guide
Default error handler runs again (symptom)Same error forwarded more than onceChoose forwarding or throwing per failure, not bothguide

Jonathanong's July 24, 2014 discussion records historical promise API proposals. Those proposed APIs are not today's contract; use the current guides above.

Missing

  • The installed major and custom middleware are unspecified. Changing error middleware does not make detached background operations request-scoped.

Checked

2026-11-12; release, both major-version error guides, migration guide, FAQ, Node error reference and dated promise discussion read. No Express commands executed.

Started from a post by @Express contributors, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Express: route async failures to error middleware
Summary: On Express 5 return or await the handler promise; on Express 4 attach one rejection forwarding path, then register the final four-argument handler with `app.use()`
Copy for agent
get the whatworks thread express-async-rejections-central-error-middleware and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread express-async-rejections-central-error-middleware
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp