mirror of https://github.com/abpframework/abp.git
committed by
GitHub
13 changed files with 130 additions and 4 deletions
@ -1,8 +1,23 @@ |
|||||
namespace Volo.Abp.AspNetCore.ExceptionHandling; |
using System; |
||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.ExceptionHandling; |
||||
|
|
||||
public class AbpExceptionHandlingOptions |
public class AbpExceptionHandlingOptions |
||||
{ |
{ |
||||
public bool SendExceptionsDetailsToClients { get; set; } = false; |
public bool SendExceptionsDetailsToClients { get; set; } |
||||
|
|
||||
|
public bool SendStackTraceToClients { get; set; } |
||||
|
|
||||
|
public List<Type> SendExceptionDataToClientTypes { get; set; } |
||||
|
|
||||
public bool SendStackTraceToClients { get; set; } = true; |
public AbpExceptionHandlingOptions() |
||||
|
{ |
||||
|
SendExceptionsDetailsToClients = false; |
||||
|
SendStackTraceToClients = true; |
||||
|
SendExceptionDataToClientTypes = |
||||
|
[ |
||||
|
typeof(IBusinessException) |
||||
|
]; |
||||
|
} |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,17 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<Import Project="..\..\..\common.test.props" /> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>net8.0</TargetFramework> |
||||
|
<RootNamespace /> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\..\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" /> |
||||
|
<ProjectReference Include="..\..\src\Volo.Abp.ExceptionHandling\Volo.Abp.ExceptionHandling.csproj" /> |
||||
|
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" /> |
||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,11 @@ |
|||||
|
using Volo.Abp.Testing; |
||||
|
|
||||
|
namespace Volo.Abp.ExceptionHandling; |
||||
|
|
||||
|
public class AbpExceptionHandlingTestBase : AbpIntegratedTest<AbpExceptionHandlingTestModule> |
||||
|
{ |
||||
|
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
||||
|
{ |
||||
|
options.UseAutofac(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,14 @@ |
|||||
|
using Volo.Abp.Autofac; |
||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
|
namespace Volo.Abp.ExceptionHandling; |
||||
|
|
||||
|
[DependsOn( |
||||
|
typeof(AbpExceptionHandlingModule), |
||||
|
typeof(AbpTestBaseModule), |
||||
|
typeof(AbpAutofacModule) |
||||
|
)] |
||||
|
public class AbpExceptionHandlingTestModule : AbpModule |
||||
|
{ |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,53 @@ |
|||||
|
using Shouldly; |
||||
|
using Volo.Abp.AspNetCore.ExceptionHandling; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.ExceptionHandling; |
||||
|
|
||||
|
public class ExceptionToErrorInfoConverter_Tests : AbpExceptionHandlingTestBase |
||||
|
{ |
||||
|
private readonly IExceptionToErrorInfoConverter _exceptionToErrorInfoConverter; |
||||
|
|
||||
|
public ExceptionToErrorInfoConverter_Tests() |
||||
|
{ |
||||
|
_exceptionToErrorInfoConverter = GetRequiredService<IExceptionToErrorInfoConverter>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void SendExceptionDataToClientTypes_Test() |
||||
|
{ |
||||
|
var exception = new AbpException("test message") |
||||
|
{ |
||||
|
Data = |
||||
|
{ |
||||
|
["my_data"] = "my_data_value", |
||||
|
["my_data2"] = 42 |
||||
|
} |
||||
|
}; |
||||
|
var errorInfo = _exceptionToErrorInfoConverter.Convert(exception); |
||||
|
errorInfo.Data.ShouldBeNull(); |
||||
|
|
||||
|
errorInfo = _exceptionToErrorInfoConverter.Convert(exception, options => |
||||
|
{ |
||||
|
options.SendExceptionDataToClientTypes.Add(typeof(AbpException)); |
||||
|
}); |
||||
|
errorInfo.Data.ShouldNotBeNull(); |
||||
|
errorInfo.Data.Keys.Count.ShouldBe(2); |
||||
|
errorInfo.Data["my_data"].ShouldBe("my_data_value"); |
||||
|
errorInfo.Data["my_data2"].ShouldBe(42); |
||||
|
|
||||
|
var businessException = new BusinessException("test message") |
||||
|
{ |
||||
|
Data = |
||||
|
{ |
||||
|
["my_data"] = "my_data_value", |
||||
|
["my_data2"] = 42 |
||||
|
} |
||||
|
}; |
||||
|
errorInfo = _exceptionToErrorInfoConverter.Convert(businessException); |
||||
|
errorInfo.Data.ShouldNotBeNull(); |
||||
|
errorInfo.Data.Keys.Count.ShouldBe(2); |
||||
|
errorInfo.Data["my_data"].ShouldBe("my_data_value"); |
||||
|
errorInfo.Data["my_data2"].ShouldBe(42); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue