From beb95635ab7270c4beea6a6845a39108c2a8f556 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Sat, 15 Aug 2020 22:57:22 +0800 Subject: [PATCH] Create AbpExceptionHandlingOptions class --- docs/en/Exception-Handling.md | 11 +++++++++++ docs/zh-Hans/Exception-Handling.md | 11 +++++++++++ .../ExceptionHandling/AbpExceptionHandlingOptions.cs | 7 +++++++ .../DefaultExceptionToErrorInfoConverter.cs | 9 +++++---- 4 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs diff --git a/docs/en/Exception-Handling.md b/docs/en/Exception-Handling.md index 3ccade9767..01b49abf4a 100644 --- a/docs/en/Exception-Handling.md +++ b/docs/en/Exception-Handling.md @@ -320,3 +320,14 @@ Some exception types are automatically thrown by the framework: - `EntityNotFoundException` is thrown if the requested entity is not available. This is mostly thrown by [repositories](Repositories.md). You can also throw these type of exceptions in your code (although it's rarely needed). + +## Send exception details to the client + +You can send exceptions to the client via the `SendAllExceptionsToClients` property of the `AbpExceptionHandlingOptions` class: + +````csharp +services.Configure(options => +{ + options.SendAllExceptionsToClients = true; +}); +```` \ No newline at end of file diff --git a/docs/zh-Hans/Exception-Handling.md b/docs/zh-Hans/Exception-Handling.md index 3d8408b26d..841185ed71 100644 --- a/docs/zh-Hans/Exception-Handling.md +++ b/docs/zh-Hans/Exception-Handling.md @@ -300,3 +300,14 @@ services.Configure(options => - 如果请求的实体不存在,则抛出`EntityNotFoundException` 异常. 此异常大多数由 [repositories](Repositories.md) 抛出. 你同样可以在代码中抛出这些类型的异常(虽然很少需要这样做) + +## 发送异常详情到客户端 + +你可以通过 `AbpExceptionHandlingOptions` 类的 `SendAllExceptionsToClients` 属性异常发送到客户端: + +````csharp +services.Configure(options => +{ + options.SendAllExceptionsToClients = true; +}); +```` diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs new file mode 100644 index 0000000000..bd363678ec --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.AspNetCore.ExceptionHandling +{ + public class AbpExceptionHandlingOptions + { + public bool SendAllExceptionsToClients { get; set; } = false; + } +} diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs index 5bf59b0cc9..344d07d8b7 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs @@ -19,20 +19,21 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling { public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConverter, ITransientDependency { - public bool SendAllExceptionsToClients { get; set; } = false; - protected AbpExceptionLocalizationOptions LocalizationOptions { get; } + protected AbpExceptionHandlingOptions ExceptionHandlingOptions { get; } protected IStringLocalizerFactory StringLocalizerFactory { get; } protected IStringLocalizer L { get; } protected IServiceProvider ServiceProvider { get; } public DefaultExceptionToErrorInfoConverter( IOptions localizationOptions, + IOptions exceptionHandlingOptions, IStringLocalizerFactory stringLocalizerFactory, IStringLocalizer abpUiStringLocalizer, IServiceProvider serviceProvider) { ServiceProvider = serviceProvider; + ExceptionHandlingOptions = exceptionHandlingOptions.Value; StringLocalizerFactory = stringLocalizerFactory; L = abpUiStringLocalizer; LocalizationOptions = localizationOptions.Value; @@ -52,7 +53,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling protected virtual RemoteServiceErrorInfo CreateErrorInfoWithoutCode(Exception exception) { - if (SendAllExceptionsToClients) + if (ExceptionHandlingOptions.SendAllExceptionsToClients) { return CreateDetailedErrorInfoFromException(exception); } @@ -293,4 +294,4 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling return detailBuilder.ToString(); } } -} \ No newline at end of file +}