17

Let's have this controller in NestJS project:

  @Post('resetpassword')
  @HttpCode(200)
  async requestPasswordReset(
    @Body() body: RequestPasswordResetDTO,
  ): Promise<boolean> {
    try {
      return await this.authService.requestPasswordReset(body);
    } catch (e) {
      if (e instanceof EntityNotFoundError) {
        // Throw same exception format as class-validator throwing (ValidationError)
      } else throw e;
    }
  }

Dto definition:

export class RequestPasswordResetDTO {
  @IsNotEmpty()
  @IsEmail()
  public email!: string;
}

I want to throw error in ValidationError format (property, value, constraints, etc) when this.authService.requestPasswordReset(body); throws an EntityNotFoundError exception.

How I can create this error manually? Those errors are just thrown when DTO validation by class-validator fails. And those can be just static validations, not async database validations.

So the final API response format should be for example:

{
    "statusCode": 400,
    "error": "Bad Request",
    "message": [
        {
            "target": {
                "email": "not@existing.email"
            },
            "value": "not@existing.email",
            "property": "email",
            "children": [],
            "constraints": {
                "exists": "email address does not exists"
            }
        }
    ]
}

I need it to have consistent error handling :)

Baterka
  • 3,075
  • 5
  • 31
  • 60
  • You can create an async validator. See example here: https://github.com/typestack/class-validator#custom-validation-decorators – Shalom Peles Feb 18 '20 at 00:05

4 Answers4

25

When adding the ValidationPipe to your app, provide a custom exceptionFactory:

  app.useGlobalPipes(
    new ValidationPipe({
      exceptionFactory: (validationErrors: ValidationError[] = []) => {
        return new BadRequestException(validationErrors);
      },
    })
  );

This should be all you need to get the intended result.

For comparison, you can check out the original NestJS version here.

Joshua
  • 1,349
  • 15
  • 26
  • 2
    I think at one point NestJs use to always return ValidationErrors, but in the latest version they defaulted to their own exception filter. I could be wrong, but I did have an array of ValidationErrors coming across at one point. That or I changed the config somewhere. – Paden Nov 09 '20 at 21:17
  • And how do you access the values? – John Apr 19 '21 at 01:45
  • Not entirely sure what you're asking, @John. Inside the exception factory arrow function, the raw validation errors from `class-validator` can be accessed in the `validationErrors` variable. – Joshua Apr 20 '21 at 06:56
  • You may need to concatenate the constraints to pass into the Exception constructor to see the validation descriptions. – dijonkitchen Jun 07 '22 at 19:56
2

You could use an Exception Filter to create your customized response to that exception First we define the Exception Filter:

import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common';
import { Request, Response } from 'express';
// import { EntityNotFoundError } from 'wherever';

@Catch(EntityNotFoundError)
export class EntityNotFoundExceptionFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    const request = ctx.getRequest<Request>();
    const status = exception.getStatus();

    response
      .status(status)
      .json({
        "statusCode": 400,
        "error": "Bad Request",
        "message": [
          {
            "target": {},
            "property": "email",
            "children": [],
            "constraints": {
              "isEmail": "email must be an email"
            }
          },
          // other field exceptions
        ]
      });
  }
}

Then back in your controller, you use the filter:

  // ...
  import { EntityNotFoundExceptionFilter } from 'its module';
  // ...
  @Post('resetpassword')
  @HttpCode(200)
  @UseFilters(EntityNotFoundExceptionFilter)
  async requestPasswordReset(
    @Body() body: RequestPasswordResetDTO
  ): Promise<boolean> {
      return await this.authService.requestPasswordReset(body);
  }

This should work just fine.

toonday
  • 501
  • 4
  • 13
  • `EntityNotFoundError` is not same as `HttpException`, your example will fail on `exception.getStatus()` – Baterka Feb 18 '20 at 19:50
  • 3
    This is pretty bad. You would need to create a custom exception filter for every single error message in class-validator. That would be like 30+ exception filters. No thanks! – John Apr 19 '21 at 01:44
2

We can get back the exception response thrown by class-validator and set to response,

import {
  ArgumentsHost,
  BadRequestException,
  Catch,
  ExceptionFilter
} from '@nestjs/common';

@Catch()
export class ValidationFilter < T > implements ExceptionFilter {
  catch (exception: T, host: ArgumentsHost) {
    if (exception instanceof BadRequestException) {
      const response = host.switchToHttp().getResponse();
      response.status(exception.getStatus())
        .json(exception.getResponse());
    }
  }
}

Controller should look,

@Post('create')
@UsePipes(ValidationPipe)
@UseFilters(ValidationFilter)
async create(@Body() body: CreateDto) {

}
HenonoaH
  • 399
  • 2
  • 10
1

Extend the default validation pipe and overwrite the createExceptionFactory function:

import {
  ValidationError,
  ValidationPipe as NestValidationPipe,
} from '@nestjs/common';
import { HttpErrorByCode } from '@nestjs/common/utils/http-error-by-code.util';

export class ValidationPipe extends NestValidationPipe {
  public createExceptionFactory() {
    return (validationErrors: ValidationError[] = []) => {
      if (this.isDetailedOutputDisabled) {
        return new HttpErrorByCode[this.errorHttpStatusCode]();
      }
      return new HttpErrorByCode[this.errorHttpStatusCode](validationErrors);
    };
  }
}

What this does is just to remove a single line of code from the default ValidationPipe that converts the errors: https://github.com/nestjs/nest/blob/4ebe4504b938faff615a9c95abc4a185419e304c/packages/common/pipes/validation.pipe.ts#L123

Dan Lupascu
  • 308
  • 1
  • 3
  • 9