From 4f23e7623173c7e981aef665934ed3e9955da904 Mon Sep 17 00:00:00 2001 From: xiangning Date: Fri, 17 Jul 2026 16:24:18 +0800 Subject: [PATCH] =?UTF-8?q?fix(api):=20GlobalExceptionMiddleware=20?= =?UTF-8?q?=E8=A1=A5=E5=BC=82=E5=B8=B8=E6=97=A5=E5=BF=97=EF=BC=8C=E4=BE=BF?= =?UTF-8?q?=E4=BA=8E=E6=8E=92=E6=9F=A5=E9=9D=99=E9=BB=98=20500?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原实现 catch 异常后只返回通用错误信封,不记任何日志, 导致请求 500 时无法定位根因。补 LogError 记录异常详情+请求路径。 --- src/shared/BuildFlow.Shared/Api/GlobalExceptionMiddleware.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/shared/BuildFlow.Shared/Api/GlobalExceptionMiddleware.cs b/src/shared/BuildFlow.Shared/Api/GlobalExceptionMiddleware.cs index 8a44fe4..5a95cdf 100644 --- a/src/shared/BuildFlow.Shared/Api/GlobalExceptionMiddleware.cs +++ b/src/shared/BuildFlow.Shared/Api/GlobalExceptionMiddleware.cs @@ -3,6 +3,8 @@ using System.Text; using System.Text.Json; using FluentValidation; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using BuildFlow.Shared.Domain.Errors; using BuildFlow.Shared.Domain.Exceptions; @@ -37,6 +39,9 @@ public sealed class GlobalExceptionMiddleware(RequestDelegate next) { // 若响应头已发出(如流式输出已写出一部分字节),无法再改写状态码,只能丢弃 if (context.Response.HasStarted) return; + // 记录异常详情:原实现静默吞掉异常导致 500 无法排查,此处补日志 + var logger = context.RequestServices.GetService>(); + logger?.LogError(ex, "未处理异常: {Path} {Method}", context.Request.Path, context.Request.Method); await HandleExceptionAsync(context, ex); } }