I am trying to validate json request in my nestjs app using my custom validation pipe class "SchemaValidationPipe" which throws BadRequestException. My global exception filter is not catching the exception thrown from validation pipe.
If I throw exception from controller class then global exception filter is able to catch the exception.
@Injectable()
export class SchemaValidationPipe implements PipeTransform<any> {
constructor(private schema: any) {}
transform(value: any, metadata: ArgumentMetadata) {
const schemaValidator = new JsonValidator(this.schema);
schemaValidator
.validate(value)
.then((data) => {
if (data) {
const { isValid, message } = data;
if(!isValid) throw new BadRequestException( { status : '500', message : 'Validation failed' } );
}
return Promise.resolve(value);
})
.catch((err) => {
throw new BadRequestException('Validation failed');
});
}
}
@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter<HttpException> {
catch(exception: HttpException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const request = ctx.getRequest();
const status = exception.getStatus();
response.status(status).json({
statusCode: status,
timestamp: new Date().toISOString(),
path: request.url,
});
}
}
const app = await NestFactory.create<NestExpressApplication>(AppModule);
app.useGlobalFilters(new HttpExceptionFilter());