Skip to content

Commit 730079b

Browse files
committed
refactor: 에러 상세 정보는 응답하지 않고 로깅만 하도록 수정 #187
BaseException 에 에러 필드를 추가해 상세 로깅 가능하도록 수정
1 parent 1c50507 commit 730079b

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

backend/src/common/exception/BaseException.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { ExceptionType } from './ExceptionType';
44
export class BaseException extends HttpException {
55
readonly code: number;
66

7-
constructor(exceptionType: ExceptionType) {
7+
constructor(
8+
readonly exceptionType: ExceptionType,
9+
readonly error?: Error,
10+
) {
811
super(exceptionType.message, exceptionType.status);
912
this.code = exceptionType.code;
1013
}

backend/src/common/exception/filter/GlobalExceptionFilter.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class BaseExceptionFilter implements ExceptionFilter {
1818
const response = ctx.getResponse<Response>();
1919

2020
const status = exception.getStatus();
21-
logException(this.logger, exception.getMessage(), status);
21+
logException(this.logger, exception.getMessage(), status, exception.error);
2222

2323
return response.status(status).json({
2424
code: exception.getCode(),
@@ -59,14 +59,23 @@ export class HttpExceptionFilter implements ExceptionFilter {
5959
}
6060
}
6161

62-
function logException(logger: PinoLogger, message: string, status: number) {
62+
function logException(
63+
logger: PinoLogger,
64+
message: string,
65+
status: number,
66+
error?: Error,
67+
) {
6368
const WARN = 400;
6469
const ERROR = 500;
6570

6671
if (status < WARN) return;
6772
void (status < ERROR
68-
? logger.warn(`${message}`)
69-
: logger.error(`${message}`));
73+
? logger.warn(message)
74+
: logger.error({
75+
message,
76+
stack: error?.stack,
77+
name: error?.name,
78+
}));
7079
}
7180

7281
@Catch()
@@ -90,8 +99,6 @@ export class UnknownExceptionFilter implements ExceptionFilter {
9099
});
91100
}
92101

93-
console.error(exception);
94-
95102
return response.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
96103
code: -1,
97104
message: 'Internal server error',

backend/src/search/exception/ElasticSearchSaveException.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import { BaseException } from '@src/common/exception/BaseException';
22
import { HttpStatus } from '@nestjs/common';
33

44
export class ElasticSearchSaveException extends BaseException {
5-
constructor(id?: number) {
6-
const message = `${id && `[${id}]`} ElasticSearch에 데이터를 저장하는데 실패했습니다.`;
7-
super({
8-
code: 3001,
9-
message,
10-
status: HttpStatus.INTERNAL_SERVER_ERROR,
11-
});
5+
constructor(id?: number, error?: Error) {
6+
const message = `${id && `[${id}]`} ElasticSearch 데이터를 저장하는데 실패했습니다.`;
7+
super(
8+
{
9+
code: 3001,
10+
message,
11+
status: HttpStatus.INTERNAL_SERVER_ERROR,
12+
},
13+
error,
14+
);
1215
}
1316
}

backend/src/storage/exception/CloudFunctionsFetchException.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ import { HttpStatus } from '@nestjs/common';
33

44
export class CloudFunctionsFetchException extends BaseException {
55
constructor(error: Error) {
6-
super({
7-
code: 711,
8-
message: `Cloud function 과 Fetch 중 오류가 발생했습니다. : ${error.message}`,
9-
status: HttpStatus.INTERNAL_SERVER_ERROR,
10-
});
6+
super(
7+
{
8+
code: 711,
9+
message: `Cloud function 통신 중 오류가 발생했습니다.`,
10+
status: HttpStatus.INTERNAL_SERVER_ERROR,
11+
},
12+
error,
13+
);
1114
}
1215
}

0 commit comments

Comments
 (0)