Repository navigation
fix(express): send the standard body for an exception wrapping an error - #18199
Merged
Merged
Conversation
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.
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).
Merged
3 of 10 tasks
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).
4 of 12 tasks
2 of 4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
ExpressAdapter.reply()hands anErrorbody, as innew UnauthorizedException(err), toresponse.json(). Express then serializes the enumerable properties of theErrorwith 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 theExpressAdapterstill serializes theError. The two adapters answer the same code differently.Issue Number: N/A
What is the new behavior?
An
Errorbody 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 Errorand a status of 400 or above, placed after theStreamableFilebranch and before the content-type check. Plain objects, strings and arrays are sent as before, so a custom body such asnew HttpException({ message: err.message }, 401)is unchanged.Does this PR introduce a breaking change?
Behavior change: a route that threw an exception built from an
Errornow responds with the standard body instead of the properties of thatError, which is what theFastifyAdapteralready sends.Other information
Tests follow 614d1ac: one case in the adapter unit spec, and one Express case in
integration/hello-world/e2e/exceptions.spec.tsthat reuses theErrorBodyControllerthe Fastify case added. Both fail on master and pass with the fix.