feat: log all exceptions In HTTP context (#4149)
This commit is contained in:
@@ -9,11 +9,12 @@ import { Request, Response } from 'express';
|
|||||||
|
|
||||||
import { REQUEST_ID } from '../constants';
|
import { REQUEST_ID } from '../constants';
|
||||||
|
|
||||||
@Catch(HttpException)
|
@Catch()
|
||||||
export class ExceptionLogger implements ExceptionFilter {
|
export class ExceptionLogger implements ExceptionFilter {
|
||||||
private logger = new Logger('ExceptionLogger');
|
private logger = new Logger('ExceptionLogger');
|
||||||
|
|
||||||
catch(exception: Error, host: ArgumentsHost) {
|
catch(exception: Error, host: ArgumentsHost) {
|
||||||
|
// with useGlobalFilters, the context is always HTTP
|
||||||
const ctx = host.switchToHttp();
|
const ctx = host.switchToHttp();
|
||||||
const request = ctx.getRequest<Request>();
|
const request = ctx.getRequest<Request>();
|
||||||
const requestId = request?.header(REQUEST_ID);
|
const requestId = request?.header(REQUEST_ID);
|
||||||
@@ -27,11 +28,11 @@ export class ExceptionLogger implements ExceptionFilter {
|
|||||||
|
|
||||||
const response = ctx.getResponse<Response>();
|
const response = ctx.getResponse<Response>();
|
||||||
if (exception instanceof HttpException) {
|
if (exception instanceof HttpException) {
|
||||||
response.json(exception.getResponse());
|
response.status(exception.getStatus()).json(exception.getResponse());
|
||||||
} else {
|
} else {
|
||||||
response.status(500).json({
|
response.status(500).json({
|
||||||
message: exception.message,
|
|
||||||
statusCode: 500,
|
statusCode: 500,
|
||||||
|
error: exception.message,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user