Browse Source

Merge pull request #999 from colinin/exception-filter

fix(exception-handling): Check if response has started before setting…
pull/1010/head
yx lin 1 year ago
committed by GitHub
parent
commit
98d7c6f357
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 12
      apps/vue/src/utils/http/axios/checkStatus.ts
  2. 13
      aspnet-core/framework/common/LINGYUN.Abp.AspNetCore.Wrapper/LINGYUN/Abp/AspNetCore/Wrapper/DefaultHttpResponseWrapper.cs
  3. 10
      aspnet-core/framework/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionPageWrapResultFilter.cs
  4. 9
      aspnet-core/framework/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionWrapResultFilter.cs

12
apps/vue/src/utils/http/axios/checkStatus.ts

@ -83,7 +83,7 @@ export function checkStatus(
export function checkResponse(response: any): string | undefined { export function checkResponse(response: any): string | undefined {
// 会话超时 // 会话超时
if (response.status === 401) { if (response?.status === 401) {
const userStore = useUserStoreWithOut(); const userStore = useUserStoreWithOut();
userStore.setToken(undefined); userStore.setToken(undefined);
userStore.setSessionTimeout(true); userStore.setSessionTimeout(true);
@ -97,11 +97,11 @@ export function checkResponse(response: any): string | undefined {
return undefined; return undefined;
} }
if (response.data.Enterprises) { if (response?.data.Enterprises) {
return response.data.Enterprises; return response.data.Enterprises;
} }
let errorJson = response.data.error; let errorJson = response?.data.error;
// abp框架抛出异常信息 // abp框架抛出异常信息
if (response.headers['_abperrorformat'] === 'true') { if (response.headers['_abperrorformat'] === 'true') {
@ -124,18 +124,18 @@ export function checkResponse(response: any): string | undefined {
} }
// oauth错误信息 // oauth错误信息
if (response.data.error_description) { if (response?.data.error_description) {
error(response.data.error_description); error(response.data.error_description);
return response.data; return response.data;
} }
// 其他错误 // 其他错误
if (response.data.error.details) { if (response?.data.error.details) {
error(response.data.error.details); error(response.data.error.details);
return response.data.error.details; return response.data.error.details;
} }
if (response.data.error.message) { if (response?.data.error.message) {
error(response.data.error.message); error(response.data.error.message);
return response.data.error.message; return response.data.error.message;
} }

13
aspnet-core/framework/common/LINGYUN.Abp.AspNetCore.Wrapper/LINGYUN/Abp/AspNetCore/Wrapper/DefaultHttpResponseWrapper.cs

@ -1,5 +1,7 @@
using LINGYUN.Abp.Wrapper; using LINGYUN.Abp.Wrapper;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
@ -7,14 +9,20 @@ namespace LINGYUN.Abp.AspNetCore.Wrapper;
public class DefaultHttpResponseWrapper : IHttpResponseWrapper, ITransientDependency public class DefaultHttpResponseWrapper : IHttpResponseWrapper, ITransientDependency
{ {
public ILogger<DefaultHttpResponseWrapper> Logger { protected get; set; }
protected AbpWrapperOptions Options { get; } protected AbpWrapperOptions Options { get; }
public DefaultHttpResponseWrapper(IOptions<AbpWrapperOptions> options) public DefaultHttpResponseWrapper(IOptions<AbpWrapperOptions> options)
{ {
Options = options.Value; Options = options.Value;
Logger = NullLogger<DefaultHttpResponseWrapper>.Instance;
} }
public virtual void Wrap(HttpResponseWrapperContext context) public virtual void Wrap(HttpResponseWrapperContext context)
{
if (!context.HttpContext.Response.HasStarted)
{ {
context.HttpContext.Response.StatusCode = context.HttpStatusCode; context.HttpContext.Response.StatusCode = context.HttpStatusCode;
if (context.HttpHeaders != null) if (context.HttpHeaders != null)
@ -32,4 +40,9 @@ public class DefaultHttpResponseWrapper : IHttpResponseWrapper, ITransientDepend
context.HttpContext.Response.Headers.Append(AbpHttpWrapConsts.AbpWrapResult, "true"); context.HttpContext.Response.Headers.Append(AbpHttpWrapConsts.AbpWrapResult, "true");
} }
} }
else
{
Logger.LogWarning("HTTP response has already started, cannot set headers and status code!");
}
}
} }

10
aspnet-core/framework/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionPageWrapResultFilter.cs

@ -90,10 +90,6 @@ public class AbpExceptionPageWrapResultFilter: AbpExceptionPageFilter, ITransien
context.HttpContext.RequestServices, context.HttpContext.RequestServices,
statusCodFinder.GetStatusCode(context.HttpContext, context.Exception)); statusCodFinder.GetStatusCode(context.HttpContext, context.Exception));
exceptionWrapHandler.CreateFor(exceptionWrapContext).Wrap(exceptionWrapContext); 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>() var wrapperHeaders = new Dictionary<string, string>()
{ {
@ -106,8 +102,10 @@ public class AbpExceptionPageWrapResultFilter: AbpExceptionPageFilter, ITransien
httpResponseWrapper.Wrap(responseWrapperContext); httpResponseWrapper.Wrap(responseWrapperContext);
//context.HttpContext.Response.Headers.Add(AbpHttpWrapConsts.AbpWrapResult, "true"); context.Result = new ObjectResult(new WrapResult(
//context.HttpContext.Response.StatusCode = (int)wrapOptions.HttpStatusCode; exceptionWrapContext.ErrorInfo.Code,
exceptionWrapContext.ErrorInfo.Message,
exceptionWrapContext.ErrorInfo.Details));
context.Exception = null; //Handled! context.Exception = null; //Handled!
} }

9
aspnet-core/framework/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionWrapResultFilter.cs

@ -71,10 +71,6 @@ public class AbpExceptionWrapResultFilter : AbpExceptionFilter, ITransientDepend
context.HttpContext.RequestServices, context.HttpContext.RequestServices,
statusCodFinder.GetStatusCode(context.HttpContext, context.Exception)); statusCodFinder.GetStatusCode(context.HttpContext, context.Exception));
exceptionWrapHandler.CreateFor(exceptionWrapContext).Wrap(exceptionWrapContext); 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>() var wrapperHeaders = new Dictionary<string, string>()
{ {
@ -87,6 +83,11 @@ public class AbpExceptionWrapResultFilter : AbpExceptionFilter, ITransientDepend
httpResponseWrapper.Wrap(responseWrapperContext); httpResponseWrapper.Wrap(responseWrapperContext);
context.Result = new ObjectResult(new WrapResult(
exceptionWrapContext.ErrorInfo.Code,
exceptionWrapContext.ErrorInfo.Message,
exceptionWrapContext.ErrorInfo.Details));
context.Exception = null; //Handled! context.Exception = null; //Handled!
} }
} }

Loading…
Cancel
Save