Skip to content

fix(common): serialize errorCode in built-in exception responses - #17615

Merged
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
lazerg:fix/built-in-exceptions-error-code-17614
Aug 29, 2026
Merged

kamilmysliwiec merged 1 commit into
nestjs:masterfrom
lazerg:fix/built-in-exceptions-error-code-17614

Conversation

@lazerg

@lazerg lazerg commented Aug 29, 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?

#15525 added errorCode to HttpExceptionOptions and a fourth createBody parameter that merges it into the response body, but none of the built-in exceptions were updated to pass it. So throw new BadRequestException('Password is too weak', { errorCode: 'WEAK_PASSWORD' }) sets exception.errorCode yet 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?.errorCode to HttpException.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 errorCode is given, and a custom object passed as objectOrError is still returned verbatim.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

A plain new HttpException('Forbidden', 403, { errorCode }) is unchanged, since its response is a bare string and BaseExceptionFilter rebuilds the body itself. Covering that means touching @nestjs/core, so I left it out of a common-only fix, but I can add it here if you want it.

The docs example also omits the error field that the built-in exceptions always set, so it needs a small follow-up in nestjs/docs.nestjs.com.

@lazerg
lazerg force-pushed the fix/built-in-exceptions-error-code-17614 branch from 57644af to 06e39ea Compare August 29, 2026 13:45
@kamilmysliwiec
kamilmysliwiec merged commit abce26e into nestjs:master Aug 29, 2026
3 checks passed
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>
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