Skip to content

Commit

Permalink
refactor: 에러 상세 정보는 응답하지 않고 로깅만 하도록 수정 #187
Browse files Browse the repository at this point in the history
BaseException 에 에러 필드를 추가해 상세 로깅 가능하도록 수정
  • Loading branch information
Miensoap committed Nov 26, 2024
1 parent 1c50507 commit 730079b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
5 changes: 4 additions & 1 deletion backend/src/common/exception/BaseException.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { ExceptionType } from './ExceptionType';
export class BaseException extends HttpException {
readonly code: number;

constructor(exceptionType: ExceptionType) {
constructor(
readonly exceptionType: ExceptionType,
readonly error?: Error,
) {
super(exceptionType.message, exceptionType.status);
this.code = exceptionType.code;
}
Expand Down
19 changes: 13 additions & 6 deletions backend/src/common/exception/filter/GlobalExceptionFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class BaseExceptionFilter implements ExceptionFilter {
const response = ctx.getResponse<Response>();

const status = exception.getStatus();
logException(this.logger, exception.getMessage(), status);
logException(this.logger, exception.getMessage(), status, exception.error);

return response.status(status).json({
code: exception.getCode(),
Expand Down Expand Up @@ -59,14 +59,23 @@ export class HttpExceptionFilter implements ExceptionFilter {
}
}

function logException(logger: PinoLogger, message: string, status: number) {
function logException(
logger: PinoLogger,
message: string,
status: number,
error?: Error,
) {
const WARN = 400;
const ERROR = 500;

if (status < WARN) return;
void (status < ERROR
? logger.warn(`${message}`)
: logger.error(`${message}`));
? logger.warn(message)
: logger.error({
message,
stack: error?.stack,
name: error?.name,
}));
}

@Catch()
Expand All @@ -90,8 +99,6 @@ export class UnknownExceptionFilter implements ExceptionFilter {
});
}

console.error(exception);

return response.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
code: -1,
message: 'Internal server error',
Expand Down
17 changes: 10 additions & 7 deletions backend/src/search/exception/ElasticSearchSaveException.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import { BaseException } from '@src/common/exception/BaseException';
import { HttpStatus } from '@nestjs/common';

export class ElasticSearchSaveException extends BaseException {
constructor(id?: number) {
const message = `${id && `[${id}]`} ElasticSearch에 데이터를 저장하는데 실패했습니다.`;
super({
code: 3001,
message,
status: HttpStatus.INTERNAL_SERVER_ERROR,
});
constructor(id?: number, error?: Error) {
const message = `${id && `[${id}]`} ElasticSearch 데이터를 저장하는데 실패했습니다.`;
super(
{
code: 3001,
message,
status: HttpStatus.INTERNAL_SERVER_ERROR,
},
error,
);
}
}
13 changes: 8 additions & 5 deletions backend/src/storage/exception/CloudFunctionsFetchException.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { HttpStatus } from '@nestjs/common';

export class CloudFunctionsFetchException extends BaseException {
constructor(error: Error) {
super({
code: 711,
message: `Cloud function 과 Fetch 중 오류가 발생했습니다. : ${error.message}`,
status: HttpStatus.INTERNAL_SERVER_ERROR,
});
super(
{
code: 711,
message: `Cloud function 통신 중 오류가 발생했습니다.`,
status: HttpStatus.INTERNAL_SERVER_ERROR,
},
error,
);
}
}

0 comments on commit 730079b

Please sign in to comment.