Skip to content

Commit af7adfb

Browse files
authored
Merge pull request #397 from Quickchive/fix/improve-exception-filter-logging
Fix/improve exception filter logging
2 parents df15775 + a040603 commit af7adfb

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

src/common/exceptions/http-exception.filter.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
Catch,
44
ExceptionFilter,
55
HttpException,
6-
HttpStatus,
6+
HttpStatus, InternalServerErrorException,
77
} from '@nestjs/common';
88
import { Request, Response } from 'express';
99
import { logger } from '../logger';
@@ -34,7 +34,7 @@ class ErrorResponse {
3434

3535
@Catch()
3636
export class HttpExceptionFilter implements ExceptionFilter {
37-
catch(exception: HttpException, host: ArgumentsHost) {
37+
catch(exception: HttpException | Error, host: ArgumentsHost) {
3838
const ctx = host.switchToHttp();
3939
const response = ctx.getResponse<Response>();
4040
const request = ctx.getRequest<Request>();
@@ -44,12 +44,16 @@ export class HttpExceptionFilter implements ExceptionFilter {
4444
: HttpStatus.INTERNAL_SERVER_ERROR;
4545
const { ip, method, url } = request;
4646
logger.error(
47-
`${method} - ${url} - ${ip.split(':').at(-1)} - ${JSON.stringify(
47+
`${method} - ${url} - ${ip?.split(':')?.at(-1)} - ${JSON.stringify(
4848
exception,
4949
)}`,
5050
);
5151

52-
const exceptionResponse = exception.getResponse() as {
52+
if (!(exception instanceof HttpException)) {
53+
exception = new InternalServerErrorException(exception.message);
54+
}
55+
56+
const exceptionResponse = (exception as HttpException).getResponse() as {
5357
error: string;
5458
message?: string | string[];
5559
data?: any;

0 commit comments

Comments
 (0)