toonvanstrijp / nestjs-i18n

The i18n module for nestjs.
https://nestjs-i18n.com
Other
646 stars 111 forks source link

Inconsistent Error Response Structure When Using I18nValidationExceptionFilter #646

Closed Jeel-Parikh closed 4 weeks ago

Jeel-Parikh commented 2 months ago

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

rubiin commented 4 weeks ago

Available on latest version

Jeel-Parikh commented 4 weeks ago

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.