Browse Source

fix(exception-handling): Check if response has started before setting header and status code

pull/999/head
colin 1 year ago
parent
commit
3f0c410659
  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 {
// 会话超时
if (response.status === 401) {
if (response?.status === 401) {
const userStore = useUserStoreWithOut();
userStore.setToken(undefined);
userStore.setSessionTimeout(true);
@ -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') {
@ -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;
}

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

@ -1,5 +1,7 @@
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;
@ -7,14 +9,20 @@ 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)
{
if (!context.HttpContext.Response.HasStarted)
{
context.HttpContext.Response.StatusCode = context.HttpStatusCode;
if (context.HttpHeaders != null)
@ -32,4 +40,9 @@ public class DefaultHttpResponseWrapper : IHttpResponseWrapper, ITransientDepend
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,
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>()
{
@ -106,8 +102,10 @@ public class AbpExceptionPageWrapResultFilter: AbpExceptionPageFilter, ITransien
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!
}

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,
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>()
{
@ -87,6 +83,11 @@ public class AbpExceptionWrapResultFilter : AbpExceptionFilter, ITransientDepend
httpResponseWrapper.Wrap(responseWrapperContext);
context.Result = new ObjectResult(new WrapResult(
exceptionWrapContext.ErrorInfo.Code,
exceptionWrapContext.ErrorInfo.Message,
exceptionWrapContext.ErrorInfo.Details));
context.Exception = null; //Handled!
}
}

Loading…
Cancel
Save