Browse Source

Merge pull request #20812 from abpframework/SendExceptionDataToClientTypes

Only set specific type of exception's `data` to `errorInfo.Data`.
pull/20832/head
ahmetfarukulu 2 years ago
committed by GitHub
parent
commit
d75d34c0a7
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 7
      framework/Volo.Abp.sln
  2. 1
      framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/ExceptionHandling/UserExceptionInformer.cs
  3. 1
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Controllers/ErrorController.cs
  4. 1
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs
  5. 1
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs
  6. 1
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs
  7. 21
      framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs
  8. 5
      framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs
  9. 17
      framework/test/Volo.Abp.ExceptionHandling.Tests/Volo.Abp.ExceptionHandling.Tests.csproj
  10. 11
      framework/test/Volo.Abp.ExceptionHandling.Tests/Volo/Abp/ExceptionHandling/AbpExceptionHandlingTestBase.cs
  11. 14
      framework/test/Volo.Abp.ExceptionHandling.Tests/Volo/Abp/ExceptionHandling/AbpExceptionHandlingTestModule.cs
  12. 53
      framework/test/Volo.Abp.ExceptionHandling.Tests/Volo/Abp/ExceptionHandling/ExceptionToErrorInfoConverter_Tests.cs
  13. 1
      modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLogInfoToAuditLogConverter.cs

7
framework/Volo.Abp.sln

@ -471,6 +471,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.BlobStoring.Google
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.BlobStoring.Google.Tests", "test\Volo.Abp.BlobStoring.Google.Tests\Volo.Abp.BlobStoring.Google.Tests.csproj", "{40FB8907-9CF7-44D0-8B5F-538AC6DAF8B9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Abp.ExceptionHandling.Tests", "test\Volo.Abp.ExceptionHandling.Tests\Volo.Abp.ExceptionHandling.Tests.csproj", "{E50739A7-5E2F-4EB5-AEA9-554115CB9613}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -1405,6 +1407,10 @@ Global
{40FB8907-9CF7-44D0-8B5F-538AC6DAF8B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{40FB8907-9CF7-44D0-8B5F-538AC6DAF8B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{40FB8907-9CF7-44D0-8B5F-538AC6DAF8B9}.Release|Any CPU.Build.0 = Release|Any CPU
{E50739A7-5E2F-4EB5-AEA9-554115CB9613}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E50739A7-5E2F-4EB5-AEA9-554115CB9613}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E50739A7-5E2F-4EB5-AEA9-554115CB9613}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E50739A7-5E2F-4EB5-AEA9-554115CB9613}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -1642,6 +1648,7 @@ Global
{E1051CD0-9262-4869-832D-B951723F4DDE} = {5DF0E140-0513-4D0D-BE2E-3D4D85CD70E6}
{DEEB5200-BBF9-464D-9B7E-8FC035A27E94} = {5DF0E140-0513-4D0D-BE2E-3D4D85CD70E6}
{40FB8907-9CF7-44D0-8B5F-538AC6DAF8B9} = {447C8A77-E5F0-4538-8687-7383196D04EA}
{E50739A7-5E2F-4EB5-AEA9-554115CB9613} = {447C8A77-E5F0-4538-8687-7383196D04EA}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BB97ECF4-9A84-433F-A80B-2A3285BDD1D5}

1
framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/ExceptionHandling/UserExceptionInformer.cs

@ -67,6 +67,7 @@ public class UserExceptionInformer : IUserExceptionInformer, IScopedDependency
{
options.SendExceptionsDetailsToClients = Options.SendExceptionsDetailsToClients;
options.SendStackTraceToClients = Options.SendStackTraceToClients;
options.SendExceptionDataToClientTypes = Options.SendExceptionDataToClientTypes;
});
}
}

1
framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Controllers/ErrorController.cs

@ -51,6 +51,7 @@ public class ErrorController : AbpController
{
options.SendExceptionsDetailsToClients = ExceptionHandlingOptions.SendExceptionsDetailsToClients;
options.SendStackTraceToClients = ExceptionHandlingOptions.SendStackTraceToClients;
options.SendExceptionDataToClientTypes = ExceptionHandlingOptions.SendExceptionDataToClientTypes;
});
if (httpStatusCode == 0)

1
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs

@ -102,6 +102,7 @@ public class AbpExceptionFilter : IAsyncExceptionFilter, IAbpFilter, ITransientD
{
options.SendExceptionsDetailsToClients = exceptionHandlingOptions.SendExceptionsDetailsToClients;
options.SendStackTraceToClients = exceptionHandlingOptions.SendStackTraceToClients;
options.SendExceptionDataToClientTypes = exceptionHandlingOptions.SendExceptionDataToClientTypes;
});
var remoteServiceErrorInfoBuilder = new StringBuilder();

1
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs

@ -81,6 +81,7 @@ public class AbpExceptionPageFilter : IAsyncPageFilter, IAbpFilter, ITransientDe
{
options.SendExceptionsDetailsToClients = exceptionHandlingOptions.SendExceptionsDetailsToClients;
options.SendStackTraceToClients = exceptionHandlingOptions.SendStackTraceToClients;
options.SendExceptionDataToClientTypes = exceptionHandlingOptions.SendExceptionDataToClientTypes;
});
var logLevel = context.Exception!.GetLogLevel();

1
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs

@ -92,6 +92,7 @@ public class AbpExceptionHandlingMiddleware : AbpMiddlewareBase, ITransientDepen
{
options.SendExceptionsDetailsToClients = exceptionHandlingOptions.SendExceptionsDetailsToClients;
options.SendStackTraceToClients = exceptionHandlingOptions.SendStackTraceToClients;
options.SendExceptionDataToClientTypes = exceptionHandlingOptions.SendExceptionDataToClientTypes;
})
)
)

21
framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs

@ -1,8 +1,23 @@
namespace Volo.Abp.AspNetCore.ExceptionHandling;
using System;
using System.Collections.Generic;
namespace Volo.Abp.AspNetCore.ExceptionHandling;
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)
];
}
}

5
framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs

@ -132,7 +132,10 @@ public class DefaultExceptionToErrorInfoConverter : IExceptionToErrorInfoConvert
errorInfo.Message = L["InternalServerErrorMessage"];
}
errorInfo.Data = exception.Data;
if (options.SendExceptionDataToClientTypes.Any(t => t.IsInstanceOfType(exception)))
{
errorInfo.Data = exception.Data;
}
return errorInfo;
}

17
framework/test/Volo.Abp.ExceptionHandling.Tests/Volo.Abp.ExceptionHandling.Tests.csproj

@ -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>

11
framework/test/Volo.Abp.ExceptionHandling.Tests/Volo/Abp/ExceptionHandling/AbpExceptionHandlingTestBase.cs

@ -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();
}
}

14
framework/test/Volo.Abp.ExceptionHandling.Tests/Volo/Abp/ExceptionHandling/AbpExceptionHandlingTestModule.cs

@ -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
{
}

53
framework/test/Volo.Abp.ExceptionHandling.Tests/Volo/Abp/ExceptionHandling/ExceptionToErrorInfoConverter_Tests.cs

@ -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);
}
}

1
modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLogInfoToAuditLogConverter.cs

@ -57,6 +57,7 @@ public class AuditLogInfoToAuditLogConverter : IAuditLogInfoToAuditLogConverter,
{
options.SendExceptionsDetailsToClients = ExceptionHandlingOptions.SendExceptionsDetailsToClients;
options.SendStackTraceToClients = ExceptionHandlingOptions.SendStackTraceToClients;
options.SendExceptionDataToClientTypes = ExceptionHandlingOptions.SendExceptionDataToClientTypes;
}))
?? new List<RemoteServiceErrorInfo>();

Loading…
Cancel
Save