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()) {