Repository navigation
fix(common): serialize errorCode in built-in exception responses - #17615
Merged
kamilmysliwiec merged 1 commit intoAug 29, 2026
Merged
kamilmysliwiec merged 1 commit into
kamilmysliwiec merged 1 commit into
Conversation
lazerg
force-pushed
the
fix/built-in-exceptions-error-code-17614
branch
from
August 29, 2026 13:45
57644af to
06e39ea
Compare
4 of 12 tasks
4 of 12 tasks
kamilmysliwiec
pushed a commit
that referenced
this pull request
Aug 30, 2026
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 #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?
#15525 added
errorCodetoHttpExceptionOptionsand a fourthcreateBodyparameter that merges it into the response body, but none of the built-in exceptions were updated to pass it. Sothrow new BadRequestException('Password is too weak', { errorCode: 'WEAK_PASSWORD' })setsexception.errorCodeyet the serialized body still comes back as{ message, error, statusCode }, which is not what the docs describe.Issue Number: #17614
What is the new behavior?
Every built-in exception now forwards
httpExceptionOptions?.errorCodetoHttpException.createBody, so the code ends up in the response body:{ "message": "Password is too weak", "error": "Bad Request", "errorCode": "WEAK_PASSWORD", "statusCode": 400 }Nothing changes when no
errorCodeis given, and a custom object passed asobjectOrErroris still returned verbatim.Does this PR introduce a breaking change?
Other information
A plain
new HttpException('Forbidden', 403, { errorCode })is unchanged, since its response is a bare string andBaseExceptionFilterrebuilds the body itself. Covering that means touching@nestjs/core, so I left it out of acommon-only fix, but I can add it here if you want it.The docs example also omits the
errorfield that the built-in exceptions always set, so it needs a small follow-up in nestjs/docs.nestjs.com.