Browse Source

fix(wrapper): 异常包装器应在处理结果时判断是否需要包装

pull/407/head
cKey 4 years ago
parent
commit
0a922f7a55
  1. 3
      aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/AbpAspNetCoreMvcWrapperModule.cs
  2. 9
      aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionPageWrapResultFilter.cs
  3. 12
      aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionWrapResultFilter.cs
  4. 8
      aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/WrapResultChecker.cs
  5. 2
      aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs
  6. 16
      aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController.cs
  7. 8
      aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController_Tests.cs

3
aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/AbpAspNetCoreMvcWrapperModule.cs

@ -8,6 +8,7 @@ using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc.ApiExploring;
using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations;
using Volo.Abp.AspNetCore.Mvc.ProxyScripting;
using Volo.Abp.Content;
using Volo.Abp.Http.Modeling;
using Volo.Abp.Localization;
using Volo.Abp.Modularity;
@ -47,6 +48,8 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Wrapper
options.IgnoreReturnTypes.Add<ApplicationApiDescriptionModel>();
// api/abp/application-configuration
options.IgnoreReturnTypes.Add<ApplicationConfigurationDto>();
//
options.IgnoreReturnTypes.Add<IRemoteStreamContent>();
// Abp/ServiceProxyScript
options.IgnoreControllers.Add<AbpServiceProxyScriptController>();

9
aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionPageWrapResultFilter.cs

@ -21,15 +21,6 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Wrapper.ExceptionHandling
[ExposeServices(typeof(AbpExceptionPageFilter))]
public class AbpExceptionPageWrapResultFilter: AbpExceptionPageFilter, ITransientDependency
{
protected override bool ShouldHandleException(PageHandlerExecutingContext context)
{
if (context.ActionDescriptor.CanWarpRsult())
{
return true;
}
return base.ShouldHandleException(context);
}
protected override async Task HandleAndWrapException(PageHandlerExecutedContext context)
{
var wrapResultChecker = context.GetRequiredService<IWrapResultChecker>();

12
aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionWrapResultFilter.cs

@ -22,19 +22,15 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Wrapper.ExceptionHandling
[ExposeServices(typeof(AbpExceptionFilter))]
public class AbpExceptionWrapResultFilter : AbpExceptionFilter, ITransientDependency
{
protected override bool ShouldHandleException(ExceptionContext context)
protected override async Task HandleAndWrapException(ExceptionContext context)
{
var wrapResultChecker = context.GetRequiredService<IWrapResultChecker>();
if (wrapResultChecker.WrapOnException(context))
if (!wrapResultChecker.WrapOnException(context))
{
return true;
await base.HandleAndWrapException(context);
return;
}
return base.ShouldHandleException(context);
}
protected override async Task HandleAndWrapException(ExceptionContext context)
{
//TODO: Trigger an AbpExceptionHandled event or something like that.
var wrapResultOptions = context.GetRequiredService<IOptions<AbpAspNetCoreMvcWrapperOptions>>().Value;
var exceptionHandlingOptions = context.GetRequiredService<IOptions<AbpExceptionHandlingOptions>>().Value;

8
aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/WrapResultChecker.cs

@ -9,6 +9,7 @@ using System.Linq;
using System.Reflection;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Http;
using Volo.Abp.Threading;
namespace LINGYUN.Abp.AspNetCore.Mvc.Wrapper
{
@ -126,13 +127,14 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Wrapper
protected virtual bool CheckForReturnType(ControllerActionDescriptor controllerActionDescriptor)
{
if (controllerActionDescriptor.MethodInfo.ReturnType.IsDefined(typeof(IgnoreWrapResultAttribute), true))
var returnType = AsyncHelper.UnwrapTask(controllerActionDescriptor.MethodInfo.ReturnType);
if (returnType.IsDefined(typeof(IgnoreWrapResultAttribute), true))
{
return false;
}
return !Options.IgnoreReturnTypes.Any(type =>
controllerActionDescriptor.MethodInfo.ReturnType.IsAssignableFrom(type));
return !Options.IgnoreReturnTypes.Any(type => returnType.IsAssignableFrom(type));
}
protected virtual bool CheckForException(Exception exception)

2
aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs

@ -15,6 +15,7 @@ using Volo.Abp.AspNetCore.Mvc.Localization;
using Volo.Abp.AspNetCore.Security.Claims;
using Volo.Abp.AspNetCore.TestBase;
using Volo.Abp.Autofac;
using Volo.Abp.Content;
using Volo.Abp.GlobalFeatures;
using Volo.Abp.Localization;
using Volo.Abp.Localization.ExceptionHandling;
@ -84,6 +85,7 @@ namespace LINGYUN.Abp.AspNetCore.Mvc
options.IgnoreControllers.Add<AbpApiDefinitionController>();
// api/abp/application-configuration
options.IgnoreReturnTypes.Add<ApplicationConfigurationDto>();
options.IgnoreReturnTypes.Add<IRemoteStreamContent>();
options.IgnorePrefixUrls.Add("/api/dont/wrap-result-test");

16
aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController.cs

@ -4,8 +4,12 @@ using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Content;
namespace LINGYUN.Abp.AspNetCore.Mvc.Results
{
@ -17,6 +21,18 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Results
LocalizationResource = typeof(MvcTestResource);
}
[HttpGet]
[Route("get-text")]
public async Task<IRemoteStreamContent> DontWrapRemoteStreamContext()
{
var textBytes = Encoding.UTF8.GetBytes("text");
var memoryStream = new MemoryStream();
await memoryStream.WriteAsync(textBytes);
return new RemoteStreamContent(memoryStream);
}
[HttpGet]
[Route("exception")]
public TestResultObject WrapBusinessException()

8
aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController_Tests.cs

@ -11,6 +11,14 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Results
{
public class WrapResultController_Tests : AbpAspNetCoreMvcTestBase
{
[Fact]
public async Task Should_Return_Not_Wrap_Result_For_Abp_Remote_Stream_Content()
{
var result = await GetResponseAsStringAsync("/api/wrap-result-test/get-text");
result.ShouldNotBeNull();
result.ShouldBe("text");
}
[Fact]
public async Task Should_Return_Not_Wrap_Result_For_Abp_Api_Definition()
{

Loading…
Cancel
Save