From 2e2b8c547b6a2a4cb95b5a68ec074f9f03702933 Mon Sep 17 00:00:00 2001 From: crazyoptimist Date: Thu, 13 Jun 2024 16:43:27 -0500 Subject: [PATCH] feat: handle notFound error, make use of logger --- .../exception-filter/all-exceptions.filter.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/modules/common/exception-filter/all-exceptions.filter.ts b/src/modules/common/exception-filter/all-exceptions.filter.ts index 24847d6..d2083a0 100644 --- a/src/modules/common/exception-filter/all-exceptions.filter.ts +++ b/src/modules/common/exception-filter/all-exceptions.filter.ts @@ -4,9 +4,10 @@ import { ArgumentsHost, HttpException, HttpStatus, + Logger, } from '@nestjs/common'; import { HttpAdapterHost } from '@nestjs/core'; -import { QueryFailedError } from 'typeorm'; +import { EntityNotFoundError, QueryFailedError } from 'typeorm'; type ResponseBody = { statusCode: number; @@ -16,6 +17,8 @@ type ResponseBody = { @Catch() export class AllExceptionsFilter implements ExceptionFilter { + private readonly logger = new Logger(AllExceptionsFilter.name); + constructor(private readonly httpAdapterHost: HttpAdapterHost) {} catch(exception: unknown, host: ArgumentsHost): void { @@ -39,7 +42,15 @@ export class AllExceptionsFilter implements ExceptionFilter { message: exception.message, error: 'Database Query Failed Error', }; + } else if (exception instanceof EntityNotFoundError) { + statusCode = HttpStatus.NOT_FOUND; + responseBody = { + statusCode, + message: exception.message, + error: 'Record Not Found Error', + }; } else { + this.logger.error(exception); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; responseBody = { statusCode,