feat: log all exceptions In HTTP context (#4149)

This commit is contained in:
X1a0t
2023-09-04 15:42:55 +08:00
committed by GitHub
parent 80195ca2a0
commit ed76417ba3

View File

@@ -9,11 +9,12 @@ import { Request, Response } from 'express';
import { REQUEST_ID } from '../constants';
@Catch(HttpException)
@Catch()
export class ExceptionLogger implements ExceptionFilter {
private logger = new Logger('ExceptionLogger');
catch(exception: Error, host: ArgumentsHost) {
// with useGlobalFilters, the context is always HTTP
const ctx = host.switchToHttp();
const request = ctx.getRequest<Request>();
const requestId = request?.header(REQUEST_ID);
@@ -27,11 +28,11 @@ export class ExceptionLogger implements ExceptionFilter {
const response = ctx.getResponse<Response>();
if (exception instanceof HttpException) {
response.json(exception.getResponse());
response.status(exception.getStatus()).json(exception.getResponse());
} else {
response.status(500).json({
message: exception.message,
statusCode: 500,
error: exception.message,
});
}
}