Browse Source
Merge pull request #407 from colinin/4.4.2
fix(wrapper): 异常包装器应在处理结果时判断是否需要包装
pull/408/head
yx lin
4 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with
38 additions and
20 deletions
-
aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/AbpAspNetCoreMvcWrapperModule.cs
-
aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionPageWrapResultFilter.cs
-
aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionWrapResultFilter.cs
-
aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/WrapResultChecker.cs
-
aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs
-
aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController.cs
-
aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/WrapResultController_Tests.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>(); |
|
|
|
|
|
|
|
|
|
|
|
@ -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>(); |
|
|
|
|
|
|
|
@ -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; |
|
|
|
|
|
|
|
@ -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) |
|
|
|
|
|
|
|
@ -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"); |
|
|
|
|
|
|
|
|
|
|
|
@ -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() |
|
|
|
|
|
|
|
@ -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() |
|
|
|
{ |
|
|
|
|