feat(common): allow passing errorCode in HttpExceptionOptions #15525
+91
−3
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?
Currently,
HttpException
provides astatus
and amessage
to describe an error. While this is effective for general error reporting, it lacks a mechanism for conveying a specific, machine-readable error cause.For example, multiple validation failures (e.g., invalid email, weak password) might all result in a
400 Bad Request
. Without a distinct error code, client applications are forced to parse the human-readable error message string to determine the specific cause, which is brittle and unreliable.Issue Number: N/A
What is the new behavior?
This PR introduces a new optional
errorCode
property toHttpExceptionOptions
. This allows developers to attach a specific, machine-readable error code to an exception.This
errorCode
is then included in the JSON response body, enabling clients to programmatically and reliably handle different error scenarios without parsing fragile message strings.Example Usage:
Example Response:
Does this PR introduce a breaking change?
This change is fully non-breaking as the
errorCode
property is optional and does not affect any existing functionality.Other information
This feature enhances NestJS's capabilities for building mature, large-scale applications by aligning it with common API design best practices.