Skip to content

fix: propagate return value from fastify.errorHandler#582

Merged
mcollina merged 1 commit into
fastify:mainfrom
abdulmunimjemal:fix/error-handler-return-value
May 18, 2026
Merged

fix: propagate return value from fastify.errorHandler#582
mcollina merged 1 commit into
fastify:mainfrom
abdulmunimjemal:fix/error-handler-return-value

Conversation

@abdulmunimjemal
Copy link
Copy Markdown
Contributor

Fixes #581.

Problem

The wrapper errorHandler registered on the static routes calls
fastify.errorHandler(error, request, reply) but discards its return
value (index.js:79).

That breaks custom error handlers that rely on Fastify's normal contract
of returning a value (or a Promise) instead of calling reply.send
themselves. In particular, an async error handler that returns its
payload never resolves the reply, because the dropped promise is never
awaited by Fastify.

Vanilla Fastify routes propagate that return value, so this was a
behavioral discrepancy specific to @fastify/static.

Fix

Return the value from fastify.errorHandler(...) so the application's
error handler can short-circuit / transform the reply exactly like it
would on a non-static route. One-line change.

Tests

Added a focused regression test in test/static.test.js that:

  • Registers an async setErrorHandler returning its payload (without
    calling reply.send).
  • Triggers an error from a preHandler so the static route's wrapper
    errorHandler runs.
  • Asserts the status code, content-type, and body returned by the user's
    error handler reach the client.

Without the fix this test times out (the dropped promise never resolves
the reply). With the fix it passes.

Checks

  • npm test — 320/320 passing, 100% line coverage retained.
  • npm run lint — clean.

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina merged commit 5b6043c into fastify:main May 18, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Return value from fastify.errorHandler is thrown away

2 participants