From f99c489ec8286854e9135a345eb861f3568687d6 Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 13 Aug 2024 11:05:18 +0800 Subject: [PATCH] Check if response has started before setting header and status code. Resolve #20483 --- .../Mvc/ExceptionHandling/AbpExceptionFilter.cs | 16 ++++++++++++---- .../ExceptionHandling/AbpExceptionPageFilter.cs | 15 +++++++++++---- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs index d2abae09024..3b60e74d784 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs @@ -75,10 +75,18 @@ await context.HttpContext.RequestServices.GetRequiredService() - .GetStatusCode(context.HttpContext, context.Exception); + if (!context.HttpContext.Response.HasStarted) + { + context.HttpContext.Response.Headers.Add(AbpHttpConsts.AbpErrorFormat, "true"); + context.HttpContext.Response.StatusCode = (int)context + .GetRequiredService() + .GetStatusCode(context.HttpContext, context.Exception); + } + else + { + var logger = context.GetService>(NullLogger.Instance)!; + logger.LogWarning("HTTP response has already started, cannot set headers and status code!"); + } context.Result = new ObjectResult(new RemoteServiceErrorResponse(remoteServiceErrorInfo)); } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs index f404f52e5da..56814718b6a 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs @@ -103,10 +103,17 @@ await context.HttpContext.RequestServices.GetRequiredService() - .GetStatusCode(context.HttpContext, context.Exception!); + if (!context.HttpContext.Response.HasStarted) + { + context.HttpContext.Response.Headers.Add(AbpHttpConsts.AbpErrorFormat, "true"); + context.HttpContext.Response.StatusCode = (int)context + .GetRequiredService() + .GetStatusCode(context.HttpContext, context.Exception!); + } + else + { + logger.LogWarning("HTTP response has already started, cannot set headers and status code!"); + } context.Result = new ObjectResult(new RemoteServiceErrorResponse(remoteServiceErrorInfo)); }