This commit is contained in:
2025-05-11 17:40:51 +08:00
parent 992721b280
commit e2f75fb4dd

View File

@@ -18,9 +18,13 @@
} }
private async Task HandleExceptionAsync(HttpContext context, Exception ex) private async Task HandleExceptionAsync(HttpContext context, Exception ex)
{
if (!context.Response.HasStarted)
{ {
context.Response.ContentType = "application/problem+json"; context.Response.ContentType = "application/problem+json";
context.Response.StatusCode = StatusCodes.Status500InternalServerError; context.Response.StatusCode = StatusCodes.Status500InternalServerError;
}
var title = ""; var title = "";
var message = ""; var message = "";
var exType = ex.GetType(); var exType = ex.GetType();
@@ -50,6 +54,8 @@
_logger.LogError(ex, "Exception"); _logger.LogError(ex, "Exception");
} }
if (!context.Response.HasStarted)
{
var acceptHeader = context.Request.Headers["Accept"].FirstOrDefault(); var acceptHeader = context.Request.Headers["Accept"].FirstOrDefault();
var isAjaxRequest = acceptHeader?.Contains("application/json") == true; var isAjaxRequest = acceptHeader?.Contains("application/json") == true;
if (isAjaxRequest) if (isAjaxRequest)
@@ -67,6 +73,7 @@
{ {
await context.Response.WriteAsync(message); await context.Response.WriteAsync(message);
} }
}
} }