Skip to content

fix: preserve original error as cause in onError handler#480

Open
xianjianlf2 wants to merge 1 commit into
fastify:mainfrom
xianjianlf2:fix/onerror-actual-error-451
Open

fix: preserve original error as cause in onError handler#480
xianjianlf2 wants to merge 1 commit into
fastify:mainfrom
xianjianlf2:fix/onerror-actual-error-451

Conversation

@xianjianlf2

Copy link
Copy Markdown

Closes #451

The onError handler received a freshly-constructed FastifyError wrapper (ServiceUnavailableError, GatewayTimeoutError, InternalServerError, …) that discarded the original upstream error. This is worst with Node's AggregateError (e.g. ECONNREFUSED to both ::1 and 127.0.0.1), whose .message is empty — so new InternalServerError(err.message) produced an empty message and no way to inspect the real cause.

This passes { cause: err } to each of the 6 error constructors in the error branch (@fastify/error v4 supports the cause option), so the original error stays accessible via error.cause while message/statusCode are preserved.

  • Added a test asserting error.cause === originalError.
  • node --test → 161 passed, lint clean.

The onError handler received a wrapped FastifyError that stripped all
information about the underlying error. This was especially visible with
AggregateError (e.g. ECONNREFUSED to both ::1 and 127.0.0.1), whose
message is always empty. Pass the original error as `cause` to every
error forwarded to onError so it stays accessible.

Closes fastify#451
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.

onError handler does not receive an actual error

1 participant