-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/add interceptors and filter (#17)
* Refactor: remove unused code * Feat: Add exception filter * Feat: Add response Interceptor * Refactor: Add DB_HOST env * Feat: Add logging interceptor * Ci: Update pull_request rule * Ci: Update yq cmd * resolve conflict * resolve conflict 222 --------- Co-authored-by: saehun <nycom13@gmail.com>
- Loading branch information
1 parent
ad4bf5f
commit 0d693e4
Showing
19 changed files
with
340 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const IS_DEV = process.env.NODE_ENV === 'development'; | ||
export const IS_STAGE = process.env.NODE_ENV === 'stage'; | ||
export const IS_PROD = process.env.NODE_ENV === 'production'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { | ||
ArgumentsHost, | ||
Catch, | ||
ExceptionFilter, | ||
HttpException, | ||
Logger, | ||
} from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
import { Response } from 'express'; | ||
|
||
import { IS_DEV } from 'src/common/constants'; | ||
|
||
type ResponseBody = { | ||
statusCode: number; | ||
message: string; | ||
error?: string; | ||
}; | ||
|
||
@Catch() | ||
export class CustomExceptionFilter implements ExceptionFilter { | ||
private readonly logger = new Logger(); | ||
constructor(private readonly configService: ConfigService) {} | ||
|
||
async catch(exception: Error, host: ArgumentsHost) { | ||
const ctx = host.switchToHttp(); | ||
const request = ctx.getRequest<Request>(); | ||
const response = ctx.getResponse<Response>(); | ||
|
||
const responseBody: ResponseBody = { | ||
statusCode: 500, | ||
message: '예상치 못한 에러가 발생했습니다. 노드팀을 채찍질 해주세요', | ||
}; | ||
|
||
if (exception instanceof HttpException) { | ||
const httpExceptionResponse = exception.getResponse() as | ||
| string | ||
| ResponseBody; | ||
responseBody.statusCode = exception.getStatus(); | ||
responseBody.message = | ||
typeof httpExceptionResponse === 'string' | ||
? httpExceptionResponse | ||
: httpExceptionResponse.message; | ||
} | ||
|
||
if (exception instanceof Error && responseBody.statusCode === 500) { | ||
this.logger.error( | ||
`api : ${request.method} ${request.url} message : ${exception.message}`, | ||
); | ||
if (!IS_DEV) { | ||
await this.handle(request, exception); | ||
} | ||
} | ||
|
||
response.status(responseBody.statusCode).json(responseBody); | ||
} | ||
|
||
private async handle(request: Request, error: Error) { | ||
//TODO: DISCORD_WEBHOOK_URL 추가 예정 | ||
const discordWebhook = this.configService.get('DISCORD_WEBHOOK_URL'); | ||
const content = this.parseError(request, error); | ||
|
||
await fetch(discordWebhook, { | ||
method: 'post', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ content }), | ||
}); | ||
} | ||
|
||
private parseError(request: Request, error: Error): string { | ||
return `노드팀 채찍 맞아라~~ 🦹🏿♀️👹🦹🏿 | ||
에러 발생 API : ${request.method} ${request.url} | ||
에러 메세지 : ${error.message} | ||
에러 위치 : ${error.stack | ||
.split('\n') | ||
.slice(0, 2) | ||
.map((message) => message.trim()) | ||
.join('\n')} | ||
당장 고쳐서 올렷! | ||
`; | ||
} | ||
} |
Oops, something went wrong.