From d3f78136589569b6f63d70064bccadc4c2f01967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E6=B1=89?= <5725748+qin_and_han_dynasties@user.noreply.gitee.com> Date: Wed, 29 Apr 2026 21:39:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E6=8F=90=E7=A4=BA=E4=B8=AD=E9=97=B4=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Build_God_Api/Build_God_Api/Program.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Build_God_Api/Build_God_Api/Program.cs b/Build_God_Api/Build_God_Api/Program.cs index c1e3822..c99e408 100644 --- a/Build_God_Api/Build_God_Api/Program.cs +++ b/Build_God_Api/Build_God_Api/Program.cs @@ -190,6 +190,22 @@ namespace Build_God_Api var app = builder.Build(); + // 全局异常处理 - 只返回错误消息,不返回堆栈 + app.UseExceptionHandler(errorApp => + { + errorApp.Run(async context => + { + context.Response.StatusCode = 500; + context.Response.ContentType = "application/json"; + + var exceptionHandlerPathFeature = context.Features.Get(); + var exception = exceptionHandlerPathFeature?.Error; + + var result = System.Text.Json.JsonSerializer.Serialize(new { message = exception?.Message ?? "服务器内部错误" }); + await context.Response.WriteAsync(result); + }); + }); + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) {