fix(identity-service): 添加全局异常过滤器日志记录以便调试
This commit is contained in:
parent
4260930a55
commit
1edbe7a9c9
|
|
@ -1,6 +1,6 @@
|
||||||
import {
|
import {
|
||||||
ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus,
|
ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus,
|
||||||
Injectable, NestInterceptor, ExecutionContext, CallHandler,
|
Injectable, NestInterceptor, ExecutionContext, CallHandler, Logger,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { Response } from 'express';
|
import { Response } from 'express';
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
|
|
@ -9,9 +9,12 @@ import { DomainError, ApplicationError } from '@/shared/exceptions/domain.except
|
||||||
|
|
||||||
@Catch()
|
@Catch()
|
||||||
export class GlobalExceptionFilter implements ExceptionFilter {
|
export class GlobalExceptionFilter implements ExceptionFilter {
|
||||||
|
private readonly logger = new Logger(GlobalExceptionFilter.name);
|
||||||
|
|
||||||
catch(exception: unknown, host: ArgumentsHost) {
|
catch(exception: unknown, host: ArgumentsHost) {
|
||||||
const ctx = host.switchToHttp();
|
const ctx = host.switchToHttp();
|
||||||
const response = ctx.getResponse<Response>();
|
const response = ctx.getResponse<Response>();
|
||||||
|
const request = ctx.getRequest();
|
||||||
|
|
||||||
let status = HttpStatus.INTERNAL_SERVER_ERROR;
|
let status = HttpStatus.INTERNAL_SERVER_ERROR;
|
||||||
let message = '服务器内部错误';
|
let message = '服务器内部错误';
|
||||||
|
|
@ -37,6 +40,22 @@ export class GlobalExceptionFilter implements ExceptionFilter {
|
||||||
message = exception.message;
|
message = exception.message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 记录错误日志
|
||||||
|
const errorLog = {
|
||||||
|
method: request.method,
|
||||||
|
url: request.url,
|
||||||
|
status,
|
||||||
|
message,
|
||||||
|
code,
|
||||||
|
stack: exception instanceof Error ? exception.stack : undefined,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (status >= 500) {
|
||||||
|
this.logger.error(`Internal server error: ${JSON.stringify(errorLog)}`);
|
||||||
|
} else {
|
||||||
|
this.logger.warn(`Client error: ${JSON.stringify(errorLog)}`);
|
||||||
|
}
|
||||||
|
|
||||||
response.status(status).json({
|
response.status(status).json({
|
||||||
success: false,
|
success: false,
|
||||||
code,
|
code,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue