Skip to content

Commit

Permalink
feat: handle notFound error, make use of logger
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyoptimist committed Jun 13, 2024
1 parent f6e5cee commit 2e2b8c5
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/modules/common/exception-filter/all-exceptions.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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,
Expand Down

0 comments on commit 2e2b8c5

Please sign in to comment.