Skip to content

fix(express): send the standard body for an exception wrapping an error - #18199

Merged
kamilmysliwiec merged 2 commits into
nestjs:masterfrom
KAPUIST:fix/express-error-body
Oct 20, 2026
Merged

kamilmysliwiec merged 2 commits into
nestjs:masterfrom
KAPUIST:fix/express-error-body

Conversation

@KAPUIST

@KAPUIST KAPUIST commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

ExpressAdapter.reply() hands an Error body, as in new UnauthorizedException(err), to response.json(). Express then serializes the enumerable properties of the Error with the status of the exception. For an HTTP client error those properties include the request config, with the headers the request was sent with.

614d1ac made FastifyAdapter.reply() send the standard body of the status in that case, and noted that the ExpressAdapter still serializes the Error. The two adapters answer the same code differently.

Issue Number: N/A

What is the new behavior?

An Error body sent with an error status gets the standard body of that status, as on Fastify:

{"statusCode":401,"message":"Unauthorized"}

The check mirrors the Fastify one: body instanceof Error and a status of 400 or above, placed after the StreamableFile branch and before the content-type check. Plain objects, strings and arrays are sent as before, so a custom body such as new HttpException({ message: err.message }, 401) is unchanged.

Does this PR introduce a breaking change?

  • Yes
  • No

Behavior change: a route that threw an exception built from an Error now responds with the standard body instead of the properties of that Error, which is what the FastifyAdapter already sends.

Other information

Tests follow 614d1ac: one case in the adapter unit spec, and one Express case in integration/hello-world/e2e/exceptions.spec.ts that reuses the ErrorBodyController the Fastify case added. Both fail on master and pass with the fix.

ExpressAdapter.reply() hands an Error body, as in
new UnauthorizedException(err), to response.json(), which serializes
the enumerable properties of the Error (such as the request config of
an HTTP client error, with its authorization header) with the status of
the exception. The FastifyAdapter sends the standard body of the status
instead.

The new unit and e2e specs expect the body the FastifyAdapter sends, so
they fail until the ExpressAdapter does the same.
ExpressAdapter.reply() handed an Error body, as in
new UnauthorizedException(err), to response.json(), which serializes the
enumerable properties of the Error with the status of the exception. For
an HTTP client error that is its request config, including the
authorization header the request was sent with.

An Error body sent with an error status now gets the standard body of
that status instead, e.g. {"statusCode":401,"message":"Unauthorized"},
as the FastifyAdapter sends since 614d1ac, so both adapters answer
alike.
@kamilmysliwiec
kamilmysliwiec merged commit 876a182 into nestjs:master Oct 20, 2026
4 checks passed
KAPUIST added a commit to KAPUIST/docs.nestjs.com that referenced this pull request Oct 20, 2026
The HttpException section says that an object passed as the `response`
argument is serialized as the response body. An `Error` instance is now
the exception on both adapters: they send the default
`{ statusCode, message }` body of the status instead, so the properties
of the error do not reach the client.

Add a hint under that sentence that says so and points to the `cause`
option for keeping the original error for logging.

See nestjs/nest@614d1ac7e (fastify) and nestjs/nest#18199 (express).
kamilmysliwiec pushed a commit to nestjs/docs.nestjs.com that referenced this pull request Oct 20, 2026
#3638)

The HttpException section says that an object passed as the `response`
argument is serialized as the response body. An `Error` instance is now
the exception on both adapters: they send the default
`{ statusCode, message }` body of the status instead, so the properties
of the error do not reach the client.

Add a hint under that sentence that says so and points to the `cause`
option for keeping the original error for logging.

See nestjs/nest@614d1ac7e (fastify) and nestjs/nest#18199 (express).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants