diff --git a/aspnet-core/LINGYUN.MicroService.Common.sln b/aspnet-core/LINGYUN.MicroService.Common.sln index d32a303cf..4466df56e 100644 --- a/aspnet-core/LINGYUN.MicroService.Common.sln +++ b/aspnet-core/LINGYUN.MicroService.Common.sln @@ -204,6 +204,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LINGYUN.Abp.AspNetCore.Test EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LINGYUN.Abp.AspNetCore.Mvc.Tests", "tests\LINGYUN.Abp.AspNetCore.Mvc.Tests\LINGYUN.Abp.AspNetCore.Mvc.Tests.csproj", "{AE5E6DE8-FC02-4633-BA49-C4B8ABADB502}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LINGYUN.Abp.Wrapper.Tests", "tests\LINGYUN.Abp.Wrapper.Tests\LINGYUN.Abp.Wrapper.Tests.csproj", "{31AED9ED-29BD-4F2F-8D3A-F00CBB9FC73C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -530,6 +532,10 @@ Global {AE5E6DE8-FC02-4633-BA49-C4B8ABADB502}.Debug|Any CPU.Build.0 = Debug|Any CPU {AE5E6DE8-FC02-4633-BA49-C4B8ABADB502}.Release|Any CPU.ActiveCfg = Release|Any CPU {AE5E6DE8-FC02-4633-BA49-C4B8ABADB502}.Release|Any CPU.Build.0 = Release|Any CPU + {31AED9ED-29BD-4F2F-8D3A-F00CBB9FC73C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {31AED9ED-29BD-4F2F-8D3A-F00CBB9FC73C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {31AED9ED-29BD-4F2F-8D3A-F00CBB9FC73C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {31AED9ED-29BD-4F2F-8D3A-F00CBB9FC73C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -632,6 +638,7 @@ Global {D72748AF-2CC8-4B5B-9710-ECDE5D812D7F} = {F55B987D-1DFF-4EB0-9949-8A7136A7B689} {BD4165DB-F8A4-4715-A05A-CC08F6A18D67} = {B86C21A4-73B7-471E-B73A-B4B905EC9435} {AE5E6DE8-FC02-4633-BA49-C4B8ABADB502} = {B86C21A4-73B7-471E-B73A-B4B905EC9435} + {31AED9ED-29BD-4F2F-8D3A-F00CBB9FC73C} = {B86C21A4-73B7-471E-B73A-B4B905EC9435} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {06C707C6-02C0-411A-AD3B-2D0E13787CB8} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN.Abp.Wrapper.csproj b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN.Abp.Wrapper.csproj index fadbb6606..547ef4912 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN.Abp.Wrapper.csproj +++ b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN.Abp.Wrapper.csproj @@ -8,7 +8,7 @@ - + diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/AbpWrapperModule.cs b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/AbpWrapperModule.cs index 883c9acde..f1c566129 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/AbpWrapperModule.cs +++ b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/AbpWrapperModule.cs @@ -1,7 +1,9 @@ -using Volo.Abp.Modularity; +using Volo.Abp.ExceptionHandling; +using Volo.Abp.Modularity; namespace LINGYUN.Abp.Wrapper { + [DependsOn(typeof(AbpExceptionHandlingModule))] public class AbpWrapperModule: AbpModule { diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/AbpWrapperOptions.cs b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/AbpWrapperOptions.cs new file mode 100644 index 000000000..9f9f1d971 --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/AbpWrapperOptions.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; + +namespace LINGYUN.Abp.Wrapper +{ + public class AbpWrapperOptions + { + /// + /// 未处理异常代码 + /// 默认: 500 + /// + public string CodeWithUnhandled { get; set; } + + internal IDictionary ExceptionHandles { get; } + + public AbpWrapperOptions() + { + CodeWithUnhandled = "500"; + ExceptionHandles = new Dictionary(); + } + + public void AddHandler(IExceptionWrapHandler handler) + where TException : Exception + { + AddHandler(typeof(TException), handler); + } + + public void AddHandler(Type exceptionType, IExceptionWrapHandler handler) + { + ExceptionHandles[exceptionType] = handler; + } + + public IExceptionWrapHandler GetHandler(Type exceptionType) + { + ExceptionHandles.TryGetValue(exceptionType, out IExceptionWrapHandler handler); + + return handler; + } + } +} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/DefaultExceptionWrapHandler.cs b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/DefaultExceptionWrapHandler.cs new file mode 100644 index 000000000..1e1a668cb --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/DefaultExceptionWrapHandler.cs @@ -0,0 +1,36 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using System; +using Volo.Abp.ExceptionHandling; + +namespace LINGYUN.Abp.Wrapper +{ + public class DefaultExceptionWrapHandler : IExceptionWrapHandler + { + public void Wrap(ExceptionWrapContext context) + { + if (context.Exception is IHasErrorCode exceptionWithErrorCode) + { + string errorCode; + if (!exceptionWithErrorCode.Code.IsNullOrWhiteSpace() && + exceptionWithErrorCode.Code.Contains(":")) + { + errorCode = exceptionWithErrorCode.Code.Split(':')[1]; + } + else + { + errorCode = exceptionWithErrorCode.Code; + } + + context.WithCode(errorCode); + } + + // 没有处理的异常代码统一用配置代码处理 + if (context.ErrorInfo.Code.IsNullOrWhiteSpace()) + { + var wrapperOptions = context.ServiceProvider.GetRequiredService>().Value; + context.WithCode(wrapperOptions.CodeWithUnhandled); + } + } + } +} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/ExceptionWrapContext.cs b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/ExceptionWrapContext.cs new file mode 100644 index 000000000..3d9ec339d --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/ExceptionWrapContext.cs @@ -0,0 +1,45 @@ +using System; +using Volo.Abp.Http; + +namespace LINGYUN.Abp.Wrapper +{ + public class ExceptionWrapContext + { + public Exception Exception { get; } + public IServiceProvider ServiceProvider { get; } + public RemoteServiceErrorInfo ErrorInfo { get; } + public ExceptionWrapContext( + Exception exception, + RemoteServiceErrorInfo errorInfo, + IServiceProvider serviceProvider) + { + Exception = exception; + ErrorInfo = errorInfo; + ServiceProvider = serviceProvider; + } + + public ExceptionWrapContext WithCode(string code) + { + ErrorInfo.Code = code; + return this; + } + + public ExceptionWrapContext WithMessage(string message) + { + ErrorInfo.Message = message; + return this; + } + + public ExceptionWrapContext WithDetails(string details) + { + ErrorInfo.Details = details; + return this; + } + + public ExceptionWrapContext WithData(string key, object value) + { + ErrorInfo.Data[key] = value; + return this; + } + } +} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/ExceptionWrapHandlerFactory.cs b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/ExceptionWrapHandlerFactory.cs new file mode 100644 index 000000000..3c39fbb2c --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/ExceptionWrapHandlerFactory.cs @@ -0,0 +1,30 @@ +using Microsoft.Extensions.Options; +using Volo.Abp.DependencyInjection; + +namespace LINGYUN.Abp.Wrapper +{ + public class ExceptionWrapHandlerFactory : IExceptionWrapHandlerFactory, ITransientDependency + { + private readonly AbpWrapperOptions _options; + + public ExceptionWrapHandlerFactory( + IOptions options) + { + _options = options.Value; + } + + public IExceptionWrapHandler CreateFor(ExceptionWrapContext context) + { + var exceptionType = context.Exception.GetType(); + var handler = _options.GetHandler(exceptionType); + if (handler == null) + { + handler = new DefaultExceptionWrapHandler(); + _options.AddHandler(exceptionType, handler); + return handler; + } + + return handler; + } + } +} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/IExceptionWrapHandler.cs b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/IExceptionWrapHandler.cs new file mode 100644 index 000000000..3db9106e7 --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/IExceptionWrapHandler.cs @@ -0,0 +1,7 @@ +namespace LINGYUN.Abp.Wrapper +{ + public interface IExceptionWrapHandler + { + void Wrap(ExceptionWrapContext context); + } +} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/IExceptionWrapHandlerFactory.cs b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/IExceptionWrapHandlerFactory.cs new file mode 100644 index 000000000..36c0bb441 --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.Wrapper/LINGYUN/Abp/Wrapper/IExceptionWrapHandlerFactory.cs @@ -0,0 +1,9 @@ +using System; + +namespace LINGYUN.Abp.Wrapper +{ + public interface IExceptionWrapHandlerFactory + { + IExceptionWrapHandler CreateFor(ExceptionWrapContext context); + } +} diff --git a/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionPageWrapResultFilter.cs b/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionPageWrapResultFilter.cs index 6a62f7589..9913c1046 100644 --- a/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionPageWrapResultFilter.cs +++ b/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionPageWrapResultFilter.cs @@ -41,8 +41,6 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Wrapper.ExceptionHandling remoteServiceErrorInfoBuilder.AppendLine($"---------- {nameof(RemoteServiceErrorInfo)} ----------"); remoteServiceErrorInfoBuilder.AppendLine(context.GetRequiredService().Serialize(remoteServiceErrorInfo, indented: true)); - context.HttpContext.Response.Headers.Add(AbpHttpWrapConsts.AbpWrapResult, "true"); - var logger = context.GetService>(NullLogger.Instance); logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString()); @@ -50,21 +48,17 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Wrapper.ExceptionHandling await context.GetRequiredService().NotifyAsync(new ExceptionNotificationContext(context.Exception)); - // Warp Error Response - string errorCode = remoteServiceErrorInfo.Code; - if (context.Exception is IHasErrorCode exceptionWithErrorCode) - { - if (!exceptionWithErrorCode.Code.IsNullOrWhiteSpace() && - exceptionWithErrorCode.Code.Contains(":")) - { - errorCode = exceptionWithErrorCode.Code.Split(':')[1]; - } - else - { - errorCode = exceptionWithErrorCode.Code; - } - } - context.Result = new ObjectResult(new WrapResult(errorCode, remoteServiceErrorInfo.Message, remoteServiceErrorInfo.Details)); + var exceptionWrapHandler = context.GetRequiredService(); + var exceptionWrapContext = new ExceptionWrapContext(context.Exception, remoteServiceErrorInfo, context.HttpContext.RequestServices); + exceptionWrapHandler.CreateFor(exceptionWrapContext).Wrap(exceptionWrapContext); + var wrapResult = new WrapResult( + exceptionWrapContext.ErrorInfo.Code, + exceptionWrapContext.ErrorInfo.Message, + exceptionWrapContext.ErrorInfo.Details); + context.Result = new ObjectResult(wrapResult); + + context.HttpContext.Response.Headers.Add(AbpHttpWrapConsts.AbpWrapResult, "true"); + context.HttpContext.Response.StatusCode = (int)wrapResultOptions.HttpStatusCode; context.Exception = null; //Handled! } diff --git a/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionWrapResultFilter.cs b/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionWrapResultFilter.cs index e5bf849d8..983975ec2 100644 --- a/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionWrapResultFilter.cs +++ b/aspnet-core/modules/mvc/LINGYUN.Abp.AspNetCore.Mvc.Wrapper/LINGYUN/Abp/AspNetCore/Mvc/Wrapper/ExceptionHandling/AbpExceptionWrapResultFilter.cs @@ -51,25 +51,18 @@ namespace LINGYUN.Abp.AspNetCore.Mvc.Wrapper.ExceptionHandling await context.GetRequiredService().NotifyAsync(new ExceptionNotificationContext(context.Exception)); + var exceptionWrapHandler = context.GetRequiredService(); + var exceptionWrapContext = new ExceptionWrapContext(context.Exception, remoteServiceErrorInfo, context.HttpContext.RequestServices); + exceptionWrapHandler.CreateFor(exceptionWrapContext).Wrap(exceptionWrapContext); + var wrapResult = new WrapResult( + exceptionWrapContext.ErrorInfo.Code, + exceptionWrapContext.ErrorInfo.Message, + exceptionWrapContext.ErrorInfo.Details); + context.Result = new ObjectResult(wrapResult); + context.HttpContext.Response.Headers.Add(AbpHttpWrapConsts.AbpWrapResult, "true"); context.HttpContext.Response.StatusCode = (int)wrapResultOptions.HttpStatusCode; - // Warp Error Response - string errorCode = remoteServiceErrorInfo.Code; - if (context.Exception is IHasErrorCode exceptionWithErrorCode) - { - if (!exceptionWithErrorCode.Code.IsNullOrWhiteSpace() && - exceptionWithErrorCode.Code.Contains(":")) - { - errorCode = exceptionWithErrorCode.Code.Split(':')[1]; - } - else - { - errorCode = exceptionWithErrorCode.Code; - } - } - context.Result = new ObjectResult(new WrapResult(errorCode, remoteServiceErrorInfo.Message, remoteServiceErrorInfo.Details)); - context.Exception = null; //Handled! } diff --git a/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/ThrowMiddleware.cs b/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/ThrowMiddleware.cs new file mode 100644 index 000000000..1c415ed81 --- /dev/null +++ b/aspnet-core/tests/LINGYUN.Abp.AspNetCore.Mvc.Tests/LINGYUN/Abp/AspNetCore/Mvc/Results/ThrowMiddleware.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Http; +using System.Threading.Tasks; +using Volo.Abp; +using Volo.Abp.DependencyInjection; + +namespace LINGYUN.Abp.AspNetCore.Mvc.Results +{ + public class ThrowMiddleware : IMiddleware, ITransientDependency + { + public async Task InvokeAsync(HttpContext context, RequestDelegate next) + { + if (context.Request.Path.ToString().StartsWith("/use-throw-middleware")) + { + throw new BusinessException("Test:1001"); + } + await next(context); + } + } +} diff --git a/aspnet-core/tests/LINGYUN.Abp.Wrapper.Tests/LINGYUN.Abp.Wrapper.Tests.csproj b/aspnet-core/tests/LINGYUN.Abp.Wrapper.Tests/LINGYUN.Abp.Wrapper.Tests.csproj new file mode 100644 index 000000000..c7f883542 --- /dev/null +++ b/aspnet-core/tests/LINGYUN.Abp.Wrapper.Tests/LINGYUN.Abp.Wrapper.Tests.csproj @@ -0,0 +1,18 @@ + + + + net5.0 + + false + + + + + + + + + + + + diff --git a/aspnet-core/tests/LINGYUN.Abp.Wrapper.Tests/LINGYUN/Abp/Wrapper/AbpWrapperTestsBase.cs b/aspnet-core/tests/LINGYUN.Abp.Wrapper.Tests/LINGYUN/Abp/Wrapper/AbpWrapperTestsBase.cs new file mode 100644 index 000000000..d69ddc7ff --- /dev/null +++ b/aspnet-core/tests/LINGYUN.Abp.Wrapper.Tests/LINGYUN/Abp/Wrapper/AbpWrapperTestsBase.cs @@ -0,0 +1,8 @@ +using LINGYUN.Abp.Tests; + +namespace LINGYUN.Abp.Wrapper +{ + public abstract class AbpWrapperTestsBase: AbpTestsBase + { + } +} diff --git a/aspnet-core/tests/LINGYUN.Abp.Wrapper.Tests/LINGYUN/Abp/Wrapper/AbpWrapperTestsModule.cs b/aspnet-core/tests/LINGYUN.Abp.Wrapper.Tests/LINGYUN/Abp/Wrapper/AbpWrapperTestsModule.cs new file mode 100644 index 000000000..b713675c7 --- /dev/null +++ b/aspnet-core/tests/LINGYUN.Abp.Wrapper.Tests/LINGYUN/Abp/Wrapper/AbpWrapperTestsModule.cs @@ -0,0 +1,19 @@ +using LINGYUN.Abp.Tests; +using Volo.Abp.Modularity; + +namespace LINGYUN.Abp.Wrapper +{ + [DependsOn( + typeof(AbpWrapperModule), + typeof(AbpTestsBaseModule))] + public class AbpWrapperTestsModule: AbpModule + { + public override void ConfigureServices(ServiceConfigurationContext context) + { + Configure(options => + { + options.AddHandler(new FakeExceptionWrapHandler()); + }); + } + } +} diff --git a/aspnet-core/tests/LINGYUN.Abp.Wrapper.Tests/LINGYUN/Abp/Wrapper/FakeException.cs b/aspnet-core/tests/LINGYUN.Abp.Wrapper.Tests/LINGYUN/Abp/Wrapper/FakeException.cs new file mode 100644 index 000000000..f7a7f42c7 --- /dev/null +++ b/aspnet-core/tests/LINGYUN.Abp.Wrapper.Tests/LINGYUN/Abp/Wrapper/FakeException.cs @@ -0,0 +1,16 @@ +using Volo.Abp; + +namespace LINGYUN.Abp.Wrapper +{ + public class FakeException: AbpException + { + public FakeException() + { + } + + public FakeException(string message) + : base(message) + { + } + } +} diff --git a/aspnet-core/tests/LINGYUN.Abp.Wrapper.Tests/LINGYUN/Abp/Wrapper/FakeExceptionWrapHandler.cs b/aspnet-core/tests/LINGYUN.Abp.Wrapper.Tests/LINGYUN/Abp/Wrapper/FakeExceptionWrapHandler.cs new file mode 100644 index 000000000..053bcb702 --- /dev/null +++ b/aspnet-core/tests/LINGYUN.Abp.Wrapper.Tests/LINGYUN/Abp/Wrapper/FakeExceptionWrapHandler.cs @@ -0,0 +1,12 @@ +namespace LINGYUN.Abp.Wrapper +{ + public class FakeExceptionWrapHandler : IExceptionWrapHandler + { + public void Wrap(ExceptionWrapContext context) + { + context.WithCode("1001") + .WithMessage("自定义异常处理消息") + .WithDetails("自定义异常处理消息明细"); + } + } +} diff --git a/aspnet-core/tests/LINGYUN.Abp.Wrapper.Tests/LINGYUN/Abp/Wrapper/FakeExceptionWrapHandler_Tests.cs b/aspnet-core/tests/LINGYUN.Abp.Wrapper.Tests/LINGYUN/Abp/Wrapper/FakeExceptionWrapHandler_Tests.cs new file mode 100644 index 000000000..cfe3f534b --- /dev/null +++ b/aspnet-core/tests/LINGYUN.Abp.Wrapper.Tests/LINGYUN/Abp/Wrapper/FakeExceptionWrapHandler_Tests.cs @@ -0,0 +1,75 @@ +using Shouldly; +using Volo.Abp; +using Volo.Abp.Http; +using Xunit; + +namespace LINGYUN.Abp.Wrapper.Tests +{ + public class FakeExceptionWrapHandler_Tests: AbpWrapperTestsBase + { + private readonly IExceptionWrapHandlerFactory _exceptionWrapHandlerFactory; + + public FakeExceptionWrapHandler_Tests() + { + _exceptionWrapHandlerFactory = GetRequiredService(); + } + + [Fact] + public void Should_Return_Wraped_Result_With_Fake_Exception() + { + var exception = new FakeException(); + var exceptionWrapContext = new ExceptionWrapContext( + exception, + new RemoteServiceErrorInfo(), + ServiceProvider); + + var handler = _exceptionWrapHandlerFactory.CreateFor(exceptionWrapContext); + handler.Wrap(exceptionWrapContext); + + exceptionWrapContext.ErrorInfo.Code.ShouldBe("1001"); + exceptionWrapContext.ErrorInfo.Message.ShouldBe("Զ쳣Ϣ"); + exceptionWrapContext.ErrorInfo.Details.ShouldBe("Զ쳣Ϣϸ"); + } + + [Fact] + public void Should_Return_Wraped_Result_With_Default_Exception() + { + var exception = new AbpException(); + var errorInfo = new RemoteServiceErrorInfo( + "Ĭ쳣Ϣ", + "Ĭ쳣Ϣϸ", + "1000"); + var exceptionWrapContext = new ExceptionWrapContext( + exception, + errorInfo, + ServiceProvider); + + var handler = _exceptionWrapHandlerFactory.CreateFor(exceptionWrapContext); + handler.Wrap(exceptionWrapContext); + + exceptionWrapContext.ErrorInfo.Code.ShouldBe("1000"); + exceptionWrapContext.ErrorInfo.Message.ShouldBe("Ĭ쳣Ϣ"); + exceptionWrapContext.ErrorInfo.Details.ShouldBe("Ĭ쳣Ϣϸ"); + } + + [Fact] + public void Should_Return_Wraped_Result_Code_500_With_Unhandled_Exception() + { + var exception = new AbpException(); + var errorInfo = new RemoteServiceErrorInfo( + "Ĭ쳣Ϣ", + "Ĭ쳣Ϣϸ"); + var exceptionWrapContext = new ExceptionWrapContext( + exception, + errorInfo, + ServiceProvider); + + var handler = _exceptionWrapHandlerFactory.CreateFor(exceptionWrapContext); + handler.Wrap(exceptionWrapContext); + + exceptionWrapContext.ErrorInfo.Code.ShouldBe("500"); + exceptionWrapContext.ErrorInfo.Message.ShouldBe("Ĭ쳣Ϣ"); + exceptionWrapContext.ErrorInfo.Details.ShouldBe("Ĭ쳣Ϣϸ"); + } + } +}