Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create AbpExceptionHandlingOptions class #5067

Merged
merged 2 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/en/Exception-Handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `SendExceptionsDetailsToClients` property of the `AbpExceptionHandlingOptions` class:

````csharp
services.Configure<AbpExceptionHandlingOptions>(options =>
{
options.SendExceptionsDetailsToClients = true;
});
````
11 changes: 11 additions & 0 deletions docs/zh-Hans/Exception-Handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,14 @@ services.Configure<AbpExceptionHttpStatusCodeOptions>(options =>
- 如果请求的实体不存在,则抛出`EntityNotFoundException` 异常. 此异常大多数由 [repositories](Repositories.md) 抛出.

你同样可以在代码中抛出这些类型的异常(虽然很少需要这样做)

## 发送异常详情到客户端

你可以通过 `AbpExceptionHandlingOptions` 类的 `SendExceptionsDetailsToClients` 属性异常发送到客户端:

````csharp
services.Configure<AbpExceptionHandlingOptions>(options =>
{
options.SendExceptionsDetailsToClients = true;
});
````
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Volo.Abp.AspNetCore.ExceptionHandling
{
public class AbpExceptionHandlingOptions
{
public bool SendExceptionsDetailsToClients { get; set; } = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<AbpUiResource> L { get; }
protected IServiceProvider ServiceProvider { get; }

public DefaultExceptionToErrorInfoConverter(
IOptions<AbpExceptionLocalizationOptions> localizationOptions,
IOptions<AbpExceptionHandlingOptions> exceptionHandlingOptions,
IStringLocalizerFactory stringLocalizerFactory,
IStringLocalizer<AbpUiResource> abpUiStringLocalizer,
IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
ExceptionHandlingOptions = exceptionHandlingOptions.Value;
StringLocalizerFactory = stringLocalizerFactory;
L = abpUiStringLocalizer;
LocalizationOptions = localizationOptions.Value;
Expand All @@ -52,7 +53,7 @@ public RemoteServiceErrorInfo Convert(Exception exception)

protected virtual RemoteServiceErrorInfo CreateErrorInfoWithoutCode(Exception exception)
{
if (SendAllExceptionsToClients)
if (ExceptionHandlingOptions.SendExceptionsDetailsToClients)
{
return CreateDetailedErrorInfoFromException(exception);
}
Expand Down Expand Up @@ -293,4 +294,4 @@ protected virtual string GetValidationErrorNarrative(IHasValidationErrors valida
return detailBuilder.ToString();
}
}
}
}