Nest JS Exception Overrides Pipe Implementation in all levels both global and local

756 views Asked by At

click to see code snippet

I am implementing validation in my nestjs project and i implemented Http Exception Handler on the global scope previously . Then i Used the ValidationPipe class for input validation but its gets overridden by exception filter each time a validation fails. and works just fine once i comment excepton filter

1

There are 1 answers

0
JSH On

Exception filter throw validation exception if occur. It is just that you are not handling/catching exception details. Try to fill response.Status object correctly in your exception filter class to capture exception details correctly.

    export class HttpExceptionFilter 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();
    
        const message = exception.message;
    
          response.status(status).json({statusCode: status,
          timestamp: new Date().toISOString(),
          path: request.url,
          message: exception.message   // this one give you your validation error msg
        });
      }
    }