import { ExceptionFilter , Catch , InternalServerErrorException , ArgumentsHost , HttpException } from "@nestjs/common" ; import { LoggerService } from "../../modules/logger" ; @ Catch ( ) export class GlobalExceptionFilter implements ExceptionFilter { public constructor ( private readonly logger ) { } public catch ( error , host ) { const response = host . switchToHttp ( ) . getResponse ( ) ; this . logger . error ( error ) ; if ( error instanceof HttpException ) { this . handleHttpException ( error as HttpException , response ) ; } else { this . handleUncaughtError ( error , response ) ; } } private handleHttpException ( exception , response ) { const status = exception . getStatus ( ) ; const message = exception . message . message || "Something went wrong" ; this . logger . error ( exception . getResponse ( ) ) ; if ( process . env . NODE_ENV !== "production" ) { console . error ( exception . message ) ; } response . status ( status ) . json ( { message } ) ; } private handleUncaughtError ( error , response ) { this . logger . error ( `Unhandled exception occurred` ) ; if ( process . env . NODE_ENV !== "production" ) { console . error ( error ) ; } else { this . logger . error ( ( new InternalServerErrorException ( error . message ) ) . getResponse ( ) ) ; } response . status ( 500 ) . json ( { message : "Something went wrong" , } ) ; } }