import type { ArgumentsHost , ExceptionFilter } from '@nestjs/common' ; import { Catch , UnprocessableEntityException } from '@nestjs/common' ; import { Reflector } from '@nestjs/core' ; import type { ValidationError } from 'class-validator' ; import type { Response } from 'express' ; import _ from 'lodash' ; @ Catch ( UnprocessableEntityException ) export class HttpExceptionFilter implements ExceptionFilter < UnprocessableEntityException > { constructor ( public reflector ) { } catch ( exception , host ) { const ctx = host . switchToHttp ( ) ; const response = ctx . getResponse < Response > ( ) ; const statusCode = exception . getStatus ( ) ; const r = exception . getResponse ( ) as { message : ValidationError [ ] ; } ; const validationErrors = r . message ; this . validationFilter ( validationErrors ) ; response . status ( statusCode ) . json ( r ) ; } private validationFilter ( validationErrors ) { for ( const validationError of validationErrors ) { const children = validationError . children ; if ( children && ! _ . isEmpty ( children ) ) { this . validationFilter ( children ) ; return ; } delete validationError . children ; const constraints = validationError . constraints ; if ( ! constraints ) { return ; } for ( const [ constraintKey , constraint ] of Object . entries ( constraints ) ) { if ( ! constraint ) { constraints [ constraintKey ] = `error.fields.${ _ . snakeCase ( constraintKey ) }` ; } } } } }