Skip to content

Commit

Permalink
fix(exception-handling): Check if response has started before setting…
Browse files Browse the repository at this point in the history
… header and status code
  • Loading branch information
colinin committed Sep 5, 2024
1 parent 0fa4b84 commit 3f0c410
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
12 changes: 6 additions & 6 deletions apps/vue/src/utils/http/axios/checkStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function checkStatus(

export function checkResponse(response: any): string | undefined {
// 会话超时
if (response.status === 401) {
if (response?.status === 401) {
const userStore = useUserStoreWithOut();
userStore.setToken(undefined);
userStore.setSessionTimeout(true);
Expand All @@ -97,11 +97,11 @@ export function checkResponse(response: any): string | undefined {
return undefined;
}

if (response.data.Enterprises) {
if (response?.data.Enterprises) {
return response.data.Enterprises;
}

let errorJson = response.data.error;
let errorJson = response?.data.error;

// abp框架抛出异常信息
if (response.headers['_abperrorformat'] === 'true') {
Expand All @@ -124,18 +124,18 @@ export function checkResponse(response: any): string | undefined {
}

// oauth错误信息
if (response.data.error_description) {
if (response?.data.error_description) {
error(response.data.error_description);
return response.data;
}

// 其他错误
if (response.data.error.details) {
if (response?.data.error.details) {
error(response.data.error.details);
return response.data.error.details;
}

if (response.data.error.message) {
if (response?.data.error.message) {
error(response.data.error.message);
return response.data.error.message;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,48 @@
using LINGYUN.Abp.Wrapper;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;

namespace LINGYUN.Abp.AspNetCore.Wrapper;

public class DefaultHttpResponseWrapper : IHttpResponseWrapper, ITransientDependency
{
public ILogger<DefaultHttpResponseWrapper> Logger { protected get; set; }

protected AbpWrapperOptions Options { get; }

public DefaultHttpResponseWrapper(IOptions<AbpWrapperOptions> options)
{
Options = options.Value;

Logger = NullLogger<DefaultHttpResponseWrapper>.Instance;
}

public virtual void Wrap(HttpResponseWrapperContext context)
{
context.HttpContext.Response.StatusCode = context.HttpStatusCode;
if (context.HttpHeaders != null)
if (!context.HttpContext.Response.HasStarted)
{
foreach (var header in context.HttpHeaders)
context.HttpContext.Response.StatusCode = context.HttpStatusCode;
if (context.HttpHeaders != null)
{
if (!context.HttpContext.Response.Headers.ContainsKey(header.Key))
foreach (var header in context.HttpHeaders)
{
context.HttpContext.Response.Headers.Append(header.Key, header.Value);
if (!context.HttpContext.Response.Headers.ContainsKey(header.Key))
{
context.HttpContext.Response.Headers.Append(header.Key, header.Value);
}
}
}
if (!context.HttpContext.Response.Headers.ContainsKey(AbpHttpWrapConsts.AbpWrapResult))
{
context.HttpContext.Response.Headers.Append(AbpHttpWrapConsts.AbpWrapResult, "true");
}
}
if (!context.HttpContext.Response.Headers.ContainsKey(AbpHttpWrapConsts.AbpWrapResult))
else
{
context.HttpContext.Response.Headers.Append(AbpHttpWrapConsts.AbpWrapResult, "true");
Logger.LogWarning("HTTP response has already started, cannot set headers and status code!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ await context.HttpContext.RequestServices.GetRequiredService<IAbpAuthorizationEx
context.HttpContext.RequestServices,
statusCodFinder.GetStatusCode(context.HttpContext, context.Exception));
exceptionWrapHandler.CreateFor(exceptionWrapContext).Wrap(exceptionWrapContext);
context.Result = new ObjectResult(new WrapResult(
exceptionWrapContext.ErrorInfo.Code,
exceptionWrapContext.ErrorInfo.Message,
exceptionWrapContext.ErrorInfo.Details));

var wrapperHeaders = new Dictionary<string, string>()
{
Expand All @@ -106,8 +102,10 @@ await context.HttpContext.RequestServices.GetRequiredService<IAbpAuthorizationEx

httpResponseWrapper.Wrap(responseWrapperContext);

//context.HttpContext.Response.Headers.Add(AbpHttpWrapConsts.AbpWrapResult, "true");
//context.HttpContext.Response.StatusCode = (int)wrapOptions.HttpStatusCode;
context.Result = new ObjectResult(new WrapResult(
exceptionWrapContext.ErrorInfo.Code,
exceptionWrapContext.ErrorInfo.Message,
exceptionWrapContext.ErrorInfo.Details));

context.Exception = null; //Handled!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ await context.HttpContext.RequestServices.GetRequiredService<IAbpAuthorizationEx
context.HttpContext.RequestServices,
statusCodFinder.GetStatusCode(context.HttpContext, context.Exception));
exceptionWrapHandler.CreateFor(exceptionWrapContext).Wrap(exceptionWrapContext);
context.Result = new ObjectResult(new WrapResult(
exceptionWrapContext.ErrorInfo.Code,
exceptionWrapContext.ErrorInfo.Message,
exceptionWrapContext.ErrorInfo.Details));

var wrapperHeaders = new Dictionary<string, string>()
{
Expand All @@ -87,6 +83,11 @@ await context.HttpContext.RequestServices.GetRequiredService<IAbpAuthorizationEx

httpResponseWrapper.Wrap(responseWrapperContext);

context.Result = new ObjectResult(new WrapResult(
exceptionWrapContext.ErrorInfo.Code,
exceptionWrapContext.ErrorInfo.Message,
exceptionWrapContext.ErrorInfo.Details));

context.Exception = null; //Handled!
}
}

0 comments on commit 3f0c410

Please sign in to comment.