Browse Source

Merge pull request #5067 from abpframework/liangshiwei/exceptionhandling

Create AbpExceptionHandlingOptions class
pull/5089/head
Halil İbrahim Kalkan 6 years ago
committed by GitHub
parent
commit
c6acaee035
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      docs/en/Exception-Handling.md
  2. 11
      docs/zh-Hans/Exception-Handling.md
  3. 7
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs
  4. 9
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs

11
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). - `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). 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 `SendExceptionsDetailsToClients` property of the `AbpExceptionHandlingOptions` class:
````csharp
services.Configure<AbpExceptionHandlingOptions>(options =>
{
options.SendExceptionsDetailsToClients = true;
});
````

11
docs/zh-Hans/Exception-Handling.md

@ -300,3 +300,14 @@ services.Configure<AbpExceptionHttpStatusCodeOptions>(options =>
- 如果请求的实体不存在,则抛出`EntityNotFoundException` 异常. 此异常大多数由 [repositories](Repositories.md) 抛出. - 如果请求的实体不存在,则抛出`EntityNotFoundException` 异常. 此异常大多数由 [repositories](Repositories.md) 抛出.
你同样可以在代码中抛出这些类型的异常(虽然很少需要这样做) 你同样可以在代码中抛出这些类型的异常(虽然很少需要这样做)
## 发送异常详情到客户端
你可以通过 `AbpExceptionHandlingOptions` 类的 `SendExceptionsDetailsToClients` 属性异常发送到客户端:
````csharp
services.Configure<AbpExceptionHandlingOptions>(options =>
{
options.SendExceptionsDetailsToClients = true;
});
````

7
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 SendExceptionsDetailsToClients { get; set; } = false;
}
}

9
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 class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConverter, ITransientDependency
{ {
public bool SendAllExceptionsToClients { get; set; } = false;
protected AbpExceptionLocalizationOptions LocalizationOptions { get; } protected AbpExceptionLocalizationOptions LocalizationOptions { get; }
protected AbpExceptionHandlingOptions ExceptionHandlingOptions { get; }
protected IStringLocalizerFactory StringLocalizerFactory { get; } protected IStringLocalizerFactory StringLocalizerFactory { get; }
protected IStringLocalizer<AbpUiResource> L { get; } protected IStringLocalizer<AbpUiResource> L { get; }
protected IServiceProvider ServiceProvider { get; } protected IServiceProvider ServiceProvider { get; }
public DefaultExceptionToErrorInfoConverter( public DefaultExceptionToErrorInfoConverter(
IOptions<AbpExceptionLocalizationOptions> localizationOptions, IOptions<AbpExceptionLocalizationOptions> localizationOptions,
IOptions<AbpExceptionHandlingOptions> exceptionHandlingOptions,
IStringLocalizerFactory stringLocalizerFactory, IStringLocalizerFactory stringLocalizerFactory,
IStringLocalizer<AbpUiResource> abpUiStringLocalizer, IStringLocalizer<AbpUiResource> abpUiStringLocalizer,
IServiceProvider serviceProvider) IServiceProvider serviceProvider)
{ {
ServiceProvider = serviceProvider; ServiceProvider = serviceProvider;
ExceptionHandlingOptions = exceptionHandlingOptions.Value;
StringLocalizerFactory = stringLocalizerFactory; StringLocalizerFactory = stringLocalizerFactory;
L = abpUiStringLocalizer; L = abpUiStringLocalizer;
LocalizationOptions = localizationOptions.Value; LocalizationOptions = localizationOptions.Value;
@ -52,7 +53,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling
protected virtual RemoteServiceErrorInfo CreateErrorInfoWithoutCode(Exception exception) protected virtual RemoteServiceErrorInfo CreateErrorInfoWithoutCode(Exception exception)
{ {
if (SendAllExceptionsToClients) if (ExceptionHandlingOptions.SendExceptionsDetailsToClients)
{ {
return CreateDetailedErrorInfoFromException(exception); return CreateDetailedErrorInfoFromException(exception);
} }
@ -293,4 +294,4 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling
return detailBuilder.ToString(); return detailBuilder.ToString();
} }
} }
} }

Loading…
Cancel
Save