Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add errors #31

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import express from 'express';
import { Action, useExpressServer } from 'routing-controllers';
import { UserService } from './services/UserService';
import { AuthHelper } from './utils/AuthHelper';
import { ErrorManager } from './utils/ErrorManager';

// TODO: Sentry 401, 400, 404, 403, 405, 422 코드는 에러 로깅 하지 않도록 필터 생성
Sentry.init({ dsn: process.env.SENTRY_DSN });
Sentry.init({
dsn: process.env.SENTRY_DSN,
beforeSend: event => ErrorManager.ignoreFilter(event) ? null : event,
});

const app: express.Application = express();

Expand Down Expand Up @@ -45,8 +48,8 @@ app.use(Sentry.Handlers.errorHandler());
// TODO: 모든 에러 response 같은 형태로 보내주도록 추가
app.use((err: any, _: express.Request, res: express.Response, ___: express.NextFunction) => {
if (!res.headersSent) {
// console.error(err);
res.status(err.httpCode || 500).send(err.message || 'something is wrong');
const errorBody = ErrorManager.parse(err);
res.status(errorBody.httpCode).send(errorBody.message);
}
});

Expand Down
7 changes: 7 additions & 0 deletions src/errors/UnProcessableError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { HttpError } from 'routing-controllers';

export class UnProcessableError extends HttpError {
constructor(message: string) {
super(422, message || 'request body is unprocessable');
}
}
35 changes: 35 additions & 0 deletions src/utils/ErrorManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as Sentry from '@sentry/node';
import { HttpError } from 'routing-controllers';
import { QueryFailedError } from 'typeorm';
export class ErrorManager {

// TODO: 센트리에 로깅 할 필요가 없는 에러 필터 정리, 로그인 실패, 권한 없음 등등
public static ignoreFilter(event: Sentry.Event) {
return true;
}

// TODO: 에러를 메시지로 파싱하는 객체, 에러 별로 다른 내용을 잘 보여줄 수 있도록 수정
public static parse(error: HttpError | QueryFailedError | Error) {
if (error instanceof HttpError) {
return {
httpCode: error.httpCode || 500,
name: error.name,
message: error.message || 'something is wrong',
};
}
if (error instanceof QueryFailedError) {
return {
httpCode: 400,
name: error.name,
message: error.message,
stack: error.stack,
};
}
return {
httpCode: 500,
name: error.name,
message: error.message || 'something is wrong',
trace: error.stack,
};
}
}