Skip to content

Commit

Permalink
Merge pull request #20594 from abpframework/AbpExceptionFilter
Browse files Browse the repository at this point in the history
Check if response has started before setting header and status code.
  • Loading branch information
EngincanV authored Aug 22, 2024
2 parents ec43d1b + f99c489 commit 32c68f1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,18 @@ await context.HttpContext.RequestServices.GetRequiredService<IAbpAuthorizationEx
}
else
{
context.HttpContext.Response.Headers.Add(AbpHttpConsts.AbpErrorFormat, "true");
context.HttpContext.Response.StatusCode = (int)context
.GetRequiredService<IHttpExceptionStatusCodeFinder>()
.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<IHttpExceptionStatusCodeFinder>()
.GetStatusCode(context.HttpContext, context.Exception);
}
else
{
var logger = context.GetService<ILogger<AbpExceptionFilter>>(NullLogger<AbpExceptionFilter>.Instance)!;
logger.LogWarning("HTTP response has already started, cannot set headers and status code!");
}

context.Result = new ObjectResult(new RemoteServiceErrorResponse(remoteServiceErrorInfo));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,17 @@ await context.HttpContext.RequestServices.GetRequiredService<IAbpAuthorizationEx
}
else
{
context.HttpContext.Response.Headers.Add(AbpHttpConsts.AbpErrorFormat, "true");
context.HttpContext.Response.StatusCode = (int)context
.GetRequiredService<IHttpExceptionStatusCodeFinder>()
.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<IHttpExceptionStatusCodeFinder>()
.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));
}
Expand Down

0 comments on commit 32c68f1

Please sign in to comment.