import { ArgumentsHost , Catch , HttpException , HttpStatus , Logger } from '@nestjs/common' ; import { BaseExceptionFilter } from '@nestjs/core' ; import { GqlArgumentsHost , GqlContextType , GqlExceptionFilter } from '@nestjs/graphql' ; @ Catch ( ) export class ExceptionsFilter extends BaseExceptionFilter implements GqlExceptionFilter { private readonly logger : Logger = new Logger ( ) ; public override catch ( exception , host ) { let args ; if ( host . getType < GqlContextType > ( ) === 'graphql' ) { const gqlHost = GqlArgumentsHost . create ( host ) ; const { req : { body : { operationName , variables } } } = gqlHost . getContext ( ) ; args = `${ operationName } ${ JSON . stringify ( variables ) }` ; } else { super . catch ( exception , host ) ; } const status = this . getHttpStatus ( exception ) ; if ( status === HttpStatus . INTERNAL_SERVER_ERROR ) { if ( exception instanceof Error ) { this . logger . error ( `${ exception . message }: ${ args }` , exception . stack ) ; } else { this . logger . error ( 'UnhandledException' , exception ) ; } } } private getHttpStatus ( exception ) { return exception instanceof HttpException ? exception . getStatus ( ) : HttpStatus . INTERNAL_SERVER_ERROR ; } }