From 1edbe7a9c97bb357f105f6c0e1e6fdfebe7338a6 Mon Sep 17 00:00:00 2001 From: hailin Date: Sat, 20 Dec 2025 17:36:35 -0800 Subject: [PATCH] =?UTF-8?q?fix(identity-service):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=BC=82=E5=B8=B8=E8=BF=87=E6=BB=A4=E5=99=A8?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95=E4=BB=A5=E4=BE=BF=E8=B0=83?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shared/filters/global-exception.filter.ts | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/backend/services/identity-service/src/shared/filters/global-exception.filter.ts b/backend/services/identity-service/src/shared/filters/global-exception.filter.ts index ce64c78f..05dad0fd 100644 --- a/backend/services/identity-service/src/shared/filters/global-exception.filter.ts +++ b/backend/services/identity-service/src/shared/filters/global-exception.filter.ts @@ -1,6 +1,6 @@ import { ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus, - Injectable, NestInterceptor, ExecutionContext, CallHandler, + Injectable, NestInterceptor, ExecutionContext, CallHandler, Logger, } from '@nestjs/common'; import { Response } from 'express'; import { Observable } from 'rxjs'; @@ -9,9 +9,12 @@ import { DomainError, ApplicationError } from '@/shared/exceptions/domain.except @Catch() export class GlobalExceptionFilter implements ExceptionFilter { + private readonly logger = new Logger(GlobalExceptionFilter.name); + catch(exception: unknown, host: ArgumentsHost) { const ctx = host.switchToHttp(); const response = ctx.getResponse(); + const request = ctx.getRequest(); let status = HttpStatus.INTERNAL_SERVER_ERROR; let message = '服务器内部错误'; @@ -37,6 +40,22 @@ export class GlobalExceptionFilter implements ExceptionFilter { 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({ success: false, code,