import type { ArgumentsHost , ExceptionFilter } from '@nestjs/common' ; import { Catch , HttpStatus } from '@nestjs/common' ; import { Reflector } from '@nestjs/core' ; import type { Response } from 'express' ; import { STATUS_CODES } from 'http' ; import { QueryFailedError } from 'typeorm' ; import { constraintErrors } from './constraint-errors' ; @ Catch ( QueryFailedError ) export class QueryFailedFilter implements ExceptionFilter < QueryFailedError > { constructor ( public reflector ) { } catch ( exception , host ) { const ctx = host . switchToHttp ( ) ; const response = ctx . getResponse < Response > ( ) ; const status = exception . constraint ?. startsWith ( 'UQ' ) ? HttpStatus . CONFLICT : HttpStatus . INTERNAL_SERVER_ERROR ; response . status ( status ) . json ( { statusCode : status , error : STATUS_CODES [ status ] , message : exception . constraint ? constraintErrors [ exception . constraint ] : undefined , } ) ; } }