fix(core): Serialize errorCode for plain HttpException responses - #17627
Merged
kamilmysliwiec merged 1 commit intoAug 30, 2026
Merged
Conversation
A plain HttpException constructed with a string body and an errorCode
option sets exception.errorCode but BaseExceptionFilter rebuilds the
response body from getResponse() and drops the errorCode, so the
serialized body is {statusCode:403,message:Forbidden} instead of
including errorCode.
The built-in exceptions path was fixed by nestjs#17615 (forwards
httpExceptionOptions.errorCode into createBody); this closes the same
gap for the bare-string path in @nestjs/core, as offered as a follow-up
in that PR. Object responses are preserved verbatim and the no-errorCode
case is unchanged.
Tests: two new cases in exceptions-handler.spec.ts; the errorCode one
fails without this change.
Signed-off-by: Abdel <hktitof@gmail.com>
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?
a plain
HttpExceptionbuilt with a string body and anerrorCodeoption setsexception.errorCode, butBaseExceptionFilterrebuilds the response body fromgetResponse()and drops the code, so these two behave differently today:this is the sibling gap the author of #17615 called out in that PR: "A plain
new HttpException('Forbidden', 403, { errorCode })is unchanged, since its response is a bare string andBaseExceptionFilterrebuilds the body itself... I can add it here if you want it"Issue Number: N/A (no dedicated issue, this completes the follow-up offered in #17615)
What is the new behavior?
BaseExceptionFilterkeepserrorCodein the rebuilt body when the exception response is a bare string:{ "statusCode": 403, "message": "Forbidden", "errorCode": "FORBIDDEN_ERR" }nothing changes when no
errorCodeis set, and object responses are still passed through verbatimDoes this PR introduce a breaking change?
Other information
verified: 2 new cases in
exceptions-handler.spec.ts, theerrorCodeone fails without this change, the no-errorCode one asserts the body shape stays untouched, full exceptions suites pass (26 files / 209 tests), prettier + oxlint clean