Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent Error Response Structure When Using I18nValidationExceptionFilter #646

Closed
5 tasks done
Jeel-Parikh opened this issue Jul 29, 2024 · 2 comments
Closed
5 tasks done

Comments

@Jeel-Parikh
Copy link
Contributor

Describe the bug

By default, NestJS follows the error response structure below for validation errors:
{
"message": [
"userEmail must be an email"
],
"error": "Bad Request",
"statusCode": 400
}

In this structure, the message field contains specific error details, and the error field provides a generalized HTTP status code error message.

However, after integrating I18nValidationExceptionFilter for enabling DTO validation, the response structure changes to:
{
"statusCode": 400,
"message": "Bad Request",
"errors": [
"userEmail must be an email"
]
}

In the updated structure:

The message field, which should indicate the actual reason for the error, now shows a generalized HTTP status code error message.
The error field has been renamed to errors and purpose of error was not follows nestJs default error res.
This change in the response structure causes inconsistencies, as validation errors now follow a different format compared to other exceptions.

Request:
To maintain consistency in error response structures, could the response format be standardized, particularly ensuring that the message, error field consistently provides the specific reason for the error across all exception types?

main.ts:
app.useGlobalPipes(new I18nValidationPipe({ whitelist: true }));
app.useGlobalFilters(new I18nValidationExceptionFilter({}));

user.entity.ts
@isemail(
{},
{
message: i18nValidationMessage(
'validation.INVALID_EMAIL',
),
},
)
userEmail: string;

Reproduction

app.useGlobalPipes(new I18nValidationPipe({ whitelist: true }));     app.useGlobalFilters(new I18nValidationExceptionFilter({}));

System Info

System:
    OS: Linux 6.5 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
    Memory: 2.59 GB / 15.36 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node
    npm: 10.3.0 - ~/.nvm/versions/node/v20.10.0/bin/npm
  Browsers:
    Brave Browser: 126.1.67.123
    Chrome: 126.0.6478.126

Used Package Manager

npm

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.
rubiin added a commit that referenced this issue Sep 8, 2024
Co-authored-by: Rubin Bhandari <roobin.bhandari@gmail.com>
@rubiin rubiin closed this as completed Sep 8, 2024
@rubiin
Copy link
Collaborator

rubiin commented Sep 8, 2024

Available on latest version

@Jeel-Parikh
Copy link
Contributor Author

Available on latest version

@rubiin it should come under major update as we are changing the error response according to standards.
please look into this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants