From d153bfea1bd8efbd43980aa0e410221ed0b6fb1c Mon Sep 17 00:00:00 2001 From: Berkan Sasmaz Date: Wed, 10 Nov 2021 14:24:32 +0300 Subject: [PATCH 1/3] Add an option to AbpExceptionHandlingOptions for enabling/disabling stacktrace --- .../UserExceptionInformer.cs | 6 ++- .../Controllers/ErrorController.cs | 6 ++- .../ExceptionHandling/AbpExceptionFilter.cs | 6 ++- .../AbpExceptionPageFilter.cs | 6 ++- .../AbpExceptionHandlingMiddleware.cs | 8 +++- .../AbpExceptionHandlingOptions.cs | 17 ++++++- .../DefaultExceptionToErrorInfoConverter.cs | 48 +++++++++++++++---- .../IExceptionToErrorInfoConverter.cs | 11 ++++- ...AbpAuthorizationServiceExtensions_Tests.cs | 10 ++-- .../FeatureCheckerExtensions_Tests.cs | 6 +-- ...ureNotEnableException_Localization_Test.cs | 2 +- 11 files changed, 99 insertions(+), 27 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/ExceptionHandling/UserExceptionInformer.cs b/framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/ExceptionHandling/UserExceptionInformer.cs index 127e3ef0d2..a98628e7dd 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/ExceptionHandling/UserExceptionInformer.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/ExceptionHandling/UserExceptionInformer.cs @@ -63,7 +63,11 @@ namespace Volo.Abp.AspNetCore.Components.Web.ExceptionHandling protected virtual RemoteServiceErrorInfo GetErrorInfo(UserExceptionInformerContext context) { - return ExceptionToErrorInfoConverter.Convert(context.Exception, Options.SendExceptionsDetailsToClients); + return ExceptionToErrorInfoConverter.Convert(context.Exception, options => + { + options.SendExceptionsDetailsToClients = Options.SendExceptionsDetailsToClients; + options.EnableStackTrace = Options.EnableStackTrace; + }); } } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Controllers/ErrorController.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Controllers/ErrorController.cs index 0b05941f61..365b5479f2 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Controllers/ErrorController.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Controllers/ErrorController.cs @@ -47,7 +47,11 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Controllers await _exceptionNotifier.NotifyAsync(new ExceptionNotificationContext(exception)); - var errorInfo = _errorInfoConverter.Convert(exception, _exceptionHandlingOptions.SendExceptionsDetailsToClients); + var errorInfo = _errorInfoConverter.Convert(exception, options => + { + options.SendExceptionsDetailsToClients = _exceptionHandlingOptions.SendExceptionsDetailsToClients; + options.EnableStackTrace = _exceptionHandlingOptions.EnableStackTrace; + }); if (httpStatusCode == 0) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs index 9631e94da4..0b43ead885 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs @@ -59,7 +59,11 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling var exceptionHandlingOptions = context.GetRequiredService>().Value; var exceptionToErrorInfoConverter = context.GetRequiredService(); - var remoteServiceErrorInfo = exceptionToErrorInfoConverter.Convert(context.Exception, exceptionHandlingOptions.SendExceptionsDetailsToClients); + var remoteServiceErrorInfo = exceptionToErrorInfoConverter.Convert(context.Exception, options => + { + options.SendExceptionsDetailsToClients = exceptionHandlingOptions.SendExceptionsDetailsToClients; + options.EnableStackTrace = exceptionHandlingOptions.EnableStackTrace; + }); var logLevel = context.Exception.GetLogLevel(); diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs index 2a8c136fbc..b0925facc4 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs @@ -71,7 +71,11 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling var exceptionHandlingOptions = context.GetRequiredService>().Value; var exceptionToErrorInfoConverter = context.GetRequiredService(); - var remoteServiceErrorInfo = exceptionToErrorInfoConverter.Convert(context.Exception, exceptionHandlingOptions.SendExceptionsDetailsToClients); + var remoteServiceErrorInfo = exceptionToErrorInfoConverter.Convert(context.Exception, options => + { + options.SendExceptionsDetailsToClients = exceptionHandlingOptions.SendExceptionsDetailsToClients; + options.EnableStackTrace = exceptionHandlingOptions.EnableStackTrace; + }); var logLevel = context.Exception.GetLogLevel(); diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs index 51c78b4a83..491501720a 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs @@ -76,7 +76,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling var errorInfoConverter = httpContext.RequestServices.GetRequiredService(); var statusCodeFinder = httpContext.RequestServices.GetRequiredService(); var jsonSerializer = httpContext.RequestServices.GetRequiredService(); - var options = httpContext.RequestServices.GetRequiredService>().Value; + var exceptionHandlingOptions = httpContext.RequestServices.GetRequiredService>().Value; httpContext.Response.Clear(); httpContext.Response.StatusCode = (int)statusCodeFinder.GetStatusCode(httpContext, exception); @@ -86,7 +86,11 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling await httpContext.Response.WriteAsync( jsonSerializer.Serialize( new RemoteServiceErrorResponse( - errorInfoConverter.Convert(exception, options.SendExceptionsDetailsToClients) + errorInfoConverter.Convert(exception, options => + { + options.SendExceptionsDetailsToClients = exceptionHandlingOptions.SendExceptionsDetailsToClients; + options.EnableStackTrace = exceptionHandlingOptions.EnableStackTrace; + }) ) ) ); diff --git a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs index 0cfdb8089d..8a66da1a86 100644 --- a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs +++ b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs @@ -2,6 +2,21 @@ { public class AbpExceptionHandlingOptions { - public bool SendExceptionsDetailsToClients { get; set; } = false; + public bool SendExceptionsDetailsToClients { get; set; } + + private bool _enableStackTrace; + + public bool EnableStackTrace + { + get => _enableStackTrace; + set + { + _enableStackTrace = value; + if (_enableStackTrace) + { + SendExceptionsDetailsToClients = true; + } + } + } } } diff --git a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs index eeddb2e9be..24251731a5 100644 --- a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs +++ b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs @@ -40,7 +40,11 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling public RemoteServiceErrorInfo Convert(Exception exception, bool includeSensitiveDetails) { - var errorInfo = CreateErrorInfoWithoutCode(exception, includeSensitiveDetails); + var exceptionHandlingOptions = CreateDefaultOptions(); + exceptionHandlingOptions.SendExceptionsDetailsToClients = includeSensitiveDetails; + exceptionHandlingOptions.EnableStackTrace = includeSensitiveDetails; + + var errorInfo = CreateErrorInfoWithoutCode(exception, exceptionHandlingOptions); if (exception is IHasErrorCode hasErrorCodeException) { @@ -50,11 +54,26 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling return errorInfo; } - protected virtual RemoteServiceErrorInfo CreateErrorInfoWithoutCode(Exception exception, bool includeSensitiveDetails) + public RemoteServiceErrorInfo Convert(Exception exception, Action options = null) { - if (includeSensitiveDetails) + var exceptionHandlingOptions = CreateDefaultOptions(); + options?.Invoke(exceptionHandlingOptions); + + var errorInfo = CreateErrorInfoWithoutCode(exception, exceptionHandlingOptions); + + if (exception is IHasErrorCode hasErrorCodeException) + { + errorInfo.Code = hasErrorCodeException.Code; + } + + return errorInfo; + } + + protected virtual RemoteServiceErrorInfo CreateErrorInfoWithoutCode(Exception exception, AbpExceptionHandlingOptions options) + { + if (options.SendExceptionsDetailsToClients) { - return CreateDetailedErrorInfoFromException(exception); + return CreateDetailedErrorInfoFromException(exception, options.EnableStackTrace); } exception = TryToGetActualException(exception); @@ -194,11 +213,11 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling return exception; } - protected virtual RemoteServiceErrorInfo CreateDetailedErrorInfoFromException(Exception exception) + protected virtual RemoteServiceErrorInfo CreateDetailedErrorInfoFromException(Exception exception, bool enableStackTrace) { var detailBuilder = new StringBuilder(); - AddExceptionToDetails(exception, detailBuilder); + AddExceptionToDetails(exception, detailBuilder, enableStackTrace); var errorInfo = new RemoteServiceErrorInfo(exception.Message, detailBuilder.ToString()); @@ -210,7 +229,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling return errorInfo; } - protected virtual void AddExceptionToDetails(Exception exception, StringBuilder detailBuilder) + protected virtual void AddExceptionToDetails(Exception exception, StringBuilder detailBuilder, bool enableStackTrace) { //Exception Message detailBuilder.AppendLine(exception.GetType().Name + ": " + exception.Message); @@ -237,7 +256,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling } //Exception StackTrace - if (!string.IsNullOrEmpty(exception.StackTrace)) + if (enableStackTrace && !string.IsNullOrEmpty(exception.StackTrace)) { detailBuilder.AppendLine("STACK TRACE: " + exception.StackTrace); } @@ -245,7 +264,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling //Inner exception if (exception.InnerException != null) { - AddExceptionToDetails(exception.InnerException, detailBuilder); + AddExceptionToDetails(exception.InnerException, detailBuilder, enableStackTrace); } //Inner exceptions for AggregateException @@ -259,7 +278,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling foreach (var innerException in aggException.InnerExceptions) { - AddExceptionToDetails(innerException, detailBuilder); + AddExceptionToDetails(innerException, detailBuilder, enableStackTrace); } } } @@ -296,5 +315,14 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling return detailBuilder.ToString(); } + + protected virtual AbpExceptionHandlingOptions CreateDefaultOptions() + { + return new AbpExceptionHandlingOptions + { + SendExceptionsDetailsToClients = false, + EnableStackTrace = false + }; + } } } diff --git a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/IExceptionToErrorInfoConverter.cs b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/IExceptionToErrorInfoConverter.cs index 3293be71ee..44aaec0845 100644 --- a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/IExceptionToErrorInfoConverter.cs +++ b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/IExceptionToErrorInfoConverter.cs @@ -12,9 +12,18 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling /// /// Converter method. /// - /// The exception + /// The exception. /// Should include sensitive details to the error info? /// Error info or null + [Obsolete("Use other Convert method.")] RemoteServiceErrorInfo Convert(Exception exception, bool includeSensitiveDetails); + + /// + /// Converter method. + /// + /// The exception. + /// Additional options. + /// Error info or null + RemoteServiceErrorInfo Convert(Exception exception, Action options = null); } } diff --git a/framework/test/Volo.Abp.Authorization.Tests/Microsoft/AspNetCore/Authorization/AbpAuthorizationServiceExtensions_Tests.cs b/framework/test/Volo.Abp.Authorization.Tests/Microsoft/AspNetCore/Authorization/AbpAuthorizationServiceExtensions_Tests.cs index e8876aaebf..0d3ebfa08a 100644 --- a/framework/test/Volo.Abp.Authorization.Tests/Microsoft/AspNetCore/Authorization/AbpAuthorizationServiceExtensions_Tests.cs +++ b/framework/test/Volo.Abp.Authorization.Tests/Microsoft/AspNetCore/Authorization/AbpAuthorizationServiceExtensions_Tests.cs @@ -21,27 +21,27 @@ namespace Microsoft.AspNetCore.Authorization using (CultureHelper.Use("zh-Hans")) { var exception = new AbpAuthorizationException(code: AbpAuthorizationErrorCodes.GivenPolicyHasNotGranted); - var errorInfo = _exceptionToErrorInfoConverter.Convert(exception, false); + var errorInfo = _exceptionToErrorInfoConverter.Convert(exception); errorInfo.Message.ShouldBe("授权失败! 提供的策略尚未授予."); exception = new AbpAuthorizationException(code: AbpAuthorizationErrorCodes.GivenPolicyHasNotGrantedWithPolicyName) .WithData("PolicyName", "my_policy_name"); - errorInfo = _exceptionToErrorInfoConverter.Convert(exception, false); + errorInfo = _exceptionToErrorInfoConverter.Convert(exception); errorInfo.Message.ShouldBe("授权失败! 提供的策略尚未授予: my_policy_name"); exception = new AbpAuthorizationException(code: AbpAuthorizationErrorCodes.GivenPolicyHasNotGrantedForGivenResource) .WithData("ResourceName", "my_resource_name"); - errorInfo = _exceptionToErrorInfoConverter.Convert(exception, false); + errorInfo = _exceptionToErrorInfoConverter.Convert(exception); errorInfo.Message.ShouldBe("授权失败! 提供的策略未授予提供的资源: my_resource_name"); exception = new AbpAuthorizationException(code: AbpAuthorizationErrorCodes.GivenRequirementHasNotGrantedForGivenResource) .WithData("ResourceName", "my_resource_name"); - errorInfo = _exceptionToErrorInfoConverter.Convert(exception, false); + errorInfo = _exceptionToErrorInfoConverter.Convert(exception); errorInfo.Message.ShouldBe("授权失败! 提供的要求未授予提供的资源: my_resource_name"); exception = new AbpAuthorizationException(code: AbpAuthorizationErrorCodes.GivenRequirementsHasNotGrantedForGivenResource) .WithData("ResourceName", "my_resource_name"); - errorInfo = _exceptionToErrorInfoConverter.Convert(exception, false); + errorInfo = _exceptionToErrorInfoConverter.Convert(exception); errorInfo.Message.ShouldBe("授权失败! 提供的要求未授予提供的资源: my_resource_name"); } } diff --git a/framework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/FeatureCheckerExtensions_Tests.cs b/framework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/FeatureCheckerExtensions_Tests.cs index 0df9548f3c..fd116217d4 100644 --- a/framework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/FeatureCheckerExtensions_Tests.cs +++ b/framework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/FeatureCheckerExtensions_Tests.cs @@ -22,17 +22,17 @@ namespace Volo.Abp.Features { var exception = new AbpAuthorizationException(code: AbpFeatureErrorCodes.FeatureIsNotEnabled) .WithData("FeatureName", "my_feature_name"); - var errorInfo = _exceptionToErrorInfoConverter.Convert(exception, false); + var errorInfo = _exceptionToErrorInfoConverter.Convert(exception); errorInfo.Message.ShouldBe("功能未启用: my_feature_name"); exception = new AbpAuthorizationException(code: AbpFeatureErrorCodes.AllOfTheseFeaturesMustBeEnabled) .WithData("FeatureNames", "my_feature_name, my_feature_name2"); - errorInfo = _exceptionToErrorInfoConverter.Convert(exception, false); + errorInfo = _exceptionToErrorInfoConverter.Convert(exception); errorInfo.Message.ShouldBe("必要的功能未启用. 这些功能需要启用: my_feature_name, my_feature_name2"); exception = new AbpAuthorizationException(code: AbpFeatureErrorCodes.AtLeastOneOfTheseFeaturesMustBeEnabled) .WithData("FeatureNames", "my_feature_name, my_feature_name2"); - errorInfo = _exceptionToErrorInfoConverter.Convert(exception, false); + errorInfo = _exceptionToErrorInfoConverter.Convert(exception); errorInfo.Message.ShouldBe("必要的功能未启用. 需要启用这些功能中的一项:my_feature_name, my_feature_name2"); } } diff --git a/framework/test/Volo.Abp.GlobalFeatures.Tests/Volo/Abp/GlobalFeatures/AbpGlobalFeatureNotEnableException_Localization_Test.cs b/framework/test/Volo.Abp.GlobalFeatures.Tests/Volo/Abp/GlobalFeatures/AbpGlobalFeatureNotEnableException_Localization_Test.cs index c638f03d46..b30fad3195 100644 --- a/framework/test/Volo.Abp.GlobalFeatures.Tests/Volo/Abp/GlobalFeatures/AbpGlobalFeatureNotEnableException_Localization_Test.cs +++ b/framework/test/Volo.Abp.GlobalFeatures.Tests/Volo/Abp/GlobalFeatures/AbpGlobalFeatureNotEnableException_Localization_Test.cs @@ -22,7 +22,7 @@ namespace Volo.Abp.GlobalFeatures var exception = new AbpGlobalFeatureNotEnabledException(code: AbpGlobalFeatureErrorCodes.GlobalFeatureIsNotEnabled) .WithData("ServiceName", "MyService") .WithData("GlobalFeatureName", "TestFeature");; - var errorInfo = _exceptionToErrorInfoConverter.Convert(exception, false); + var errorInfo = _exceptionToErrorInfoConverter.Convert(exception); errorInfo.Message.ShouldBe("'MyService'服务需要启用'TestFeature'功能."); } } From f75747e7e717823f9b8ac4ad9e1ea2fe80ecb057 Mon Sep 17 00:00:00 2001 From: Berkan Sasmaz Date: Wed, 10 Nov 2021 15:09:57 +0300 Subject: [PATCH 2/3] docs(Exception-Handling): add an option to `AbpExceptionHandlingOptions` for enabling/disabling StackTrace --- docs/en/Exception-Handling.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/docs/en/Exception-Handling.md b/docs/en/Exception-Handling.md index 8c84157878..2d62393ea3 100644 --- a/docs/en/Exception-Handling.md +++ b/docs/en/Exception-Handling.md @@ -321,13 +321,20 @@ Some exception types are automatically thrown by the framework: You can also throw these type of exceptions in your code (although it's rarely needed). -## Send exception details to the client +## AbpExceptionHandlingOptions -You can send exceptions to the client via the `SendExceptionsDetailsToClients` property of the `AbpExceptionHandlingOptions` class: +`AbpExceptionHandlingOptions` is the main [options object](Options.md) to configure the exception handling system. You can configure it in the `ConfigureServices` method of your [module](Module-Development-Basics.md): ````csharp -services.Configure(options => +Configure(options => { options.SendExceptionsDetailsToClients = true; + options.EnableStackTrace = false; }); ```` + +Here, a list of the options you can configure: + +* `SendExceptionsDetailsToClients` (default: `false`): You can enable or disable sending exception details to the client. +* `EnableStackTrace` (default: `false`): You can enable or disable sending the `StackTrace` of exception to the client. When you set `EnableStackTrace` to true, it is set to `true` even if `SendExceptionsDetailsToClients` is `false` because it contains the exception details. + From 3ce989eb05c1b018c312bd2fde63674f9be95c42 Mon Sep 17 00:00:00 2001 From: Berkan Sasmaz Date: Thu, 11 Nov 2021 11:28:08 +0300 Subject: [PATCH 3/3] Rename EnableStackTrace to SendStackTraceToClients --- docs/en/Exception-Handling.md | 4 ++-- .../ExceptionHandling/UserExceptionInformer.cs | 2 +- .../Controllers/ErrorController.cs | 2 +- .../ExceptionHandling/AbpExceptionFilter.cs | 2 +- .../AbpExceptionPageFilter.cs | 2 +- .../AbpExceptionHandlingMiddleware.cs | 2 +- .../AbpExceptionHandlingOptions.cs | 17 ++--------------- .../DefaultExceptionToErrorInfoConverter.cs | 18 +++++++++--------- 8 files changed, 18 insertions(+), 31 deletions(-) diff --git a/docs/en/Exception-Handling.md b/docs/en/Exception-Handling.md index 2d62393ea3..d12e9a62b6 100644 --- a/docs/en/Exception-Handling.md +++ b/docs/en/Exception-Handling.md @@ -329,12 +329,12 @@ You can also throw these type of exceptions in your code (although it's rarely n Configure(options => { options.SendExceptionsDetailsToClients = true; - options.EnableStackTrace = false; + options.SendStackTraceToClients = false; }); ```` Here, a list of the options you can configure: * `SendExceptionsDetailsToClients` (default: `false`): You can enable or disable sending exception details to the client. -* `EnableStackTrace` (default: `false`): You can enable or disable sending the `StackTrace` of exception to the client. When you set `EnableStackTrace` to true, it is set to `true` even if `SendExceptionsDetailsToClients` is `false` because it contains the exception details. +* `SendStackTraceToClients` (default: `true`): You can enable or disable sending the stack trace of exception to the client. If you want to send the stack trace to the client, you must set both `SendStackTraceToClients` and `SendExceptionsDetailsToClients` options to `true` otherwise, the stack trace will not be sent to the client. diff --git a/framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/ExceptionHandling/UserExceptionInformer.cs b/framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/ExceptionHandling/UserExceptionInformer.cs index a98628e7dd..ecbeb16e81 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/ExceptionHandling/UserExceptionInformer.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/ExceptionHandling/UserExceptionInformer.cs @@ -66,7 +66,7 @@ namespace Volo.Abp.AspNetCore.Components.Web.ExceptionHandling return ExceptionToErrorInfoConverter.Convert(context.Exception, options => { options.SendExceptionsDetailsToClients = Options.SendExceptionsDetailsToClients; - options.EnableStackTrace = Options.EnableStackTrace; + options.SendStackTraceToClients = Options.SendStackTraceToClients; }); } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Controllers/ErrorController.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Controllers/ErrorController.cs index 365b5479f2..16b6f6e325 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Controllers/ErrorController.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Controllers/ErrorController.cs @@ -50,7 +50,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Controllers var errorInfo = _errorInfoConverter.Convert(exception, options => { options.SendExceptionsDetailsToClients = _exceptionHandlingOptions.SendExceptionsDetailsToClients; - options.EnableStackTrace = _exceptionHandlingOptions.EnableStackTrace; + options.SendStackTraceToClients = _exceptionHandlingOptions.SendStackTraceToClients; }); if (httpStatusCode == 0) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs index 0b43ead885..89f3694c4f 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs @@ -62,7 +62,7 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling var remoteServiceErrorInfo = exceptionToErrorInfoConverter.Convert(context.Exception, options => { options.SendExceptionsDetailsToClients = exceptionHandlingOptions.SendExceptionsDetailsToClients; - options.EnableStackTrace = exceptionHandlingOptions.EnableStackTrace; + options.SendStackTraceToClients = exceptionHandlingOptions.SendStackTraceToClients; }); var logLevel = context.Exception.GetLogLevel(); diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs index b0925facc4..51bfd41f84 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs @@ -74,7 +74,7 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling var remoteServiceErrorInfo = exceptionToErrorInfoConverter.Convert(context.Exception, options => { options.SendExceptionsDetailsToClients = exceptionHandlingOptions.SendExceptionsDetailsToClients; - options.EnableStackTrace = exceptionHandlingOptions.EnableStackTrace; + options.SendStackTraceToClients = exceptionHandlingOptions.SendStackTraceToClients; }); var logLevel = context.Exception.GetLogLevel(); diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs index 491501720a..02a1e8f888 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs @@ -89,7 +89,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling errorInfoConverter.Convert(exception, options => { options.SendExceptionsDetailsToClients = exceptionHandlingOptions.SendExceptionsDetailsToClients; - options.EnableStackTrace = exceptionHandlingOptions.EnableStackTrace; + options.SendStackTraceToClients = exceptionHandlingOptions.SendStackTraceToClients; }) ) ) diff --git a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs index 8a66da1a86..df2073e9d8 100644 --- a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs +++ b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingOptions.cs @@ -2,21 +2,8 @@ { public class AbpExceptionHandlingOptions { - public bool SendExceptionsDetailsToClients { get; set; } - - private bool _enableStackTrace; + public bool SendExceptionsDetailsToClients { get; set; } = false; - public bool EnableStackTrace - { - get => _enableStackTrace; - set - { - _enableStackTrace = value; - if (_enableStackTrace) - { - SendExceptionsDetailsToClients = true; - } - } - } + public bool SendStackTraceToClients { get; set; } = true; } } diff --git a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs index 24251731a5..2c5feb8467 100644 --- a/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs +++ b/framework/src/Volo.Abp.ExceptionHandling/Volo/Abp/AspNetCore/ExceptionHandling/DefaultExceptionToErrorInfoConverter.cs @@ -42,7 +42,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling { var exceptionHandlingOptions = CreateDefaultOptions(); exceptionHandlingOptions.SendExceptionsDetailsToClients = includeSensitiveDetails; - exceptionHandlingOptions.EnableStackTrace = includeSensitiveDetails; + exceptionHandlingOptions.SendStackTraceToClients = includeSensitiveDetails; var errorInfo = CreateErrorInfoWithoutCode(exception, exceptionHandlingOptions); @@ -73,7 +73,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling { if (options.SendExceptionsDetailsToClients) { - return CreateDetailedErrorInfoFromException(exception, options.EnableStackTrace); + return CreateDetailedErrorInfoFromException(exception, options.SendStackTraceToClients); } exception = TryToGetActualException(exception); @@ -213,11 +213,11 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling return exception; } - protected virtual RemoteServiceErrorInfo CreateDetailedErrorInfoFromException(Exception exception, bool enableStackTrace) + protected virtual RemoteServiceErrorInfo CreateDetailedErrorInfoFromException(Exception exception, bool sendStackTraceToClients) { var detailBuilder = new StringBuilder(); - AddExceptionToDetails(exception, detailBuilder, enableStackTrace); + AddExceptionToDetails(exception, detailBuilder, sendStackTraceToClients); var errorInfo = new RemoteServiceErrorInfo(exception.Message, detailBuilder.ToString()); @@ -229,7 +229,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling return errorInfo; } - protected virtual void AddExceptionToDetails(Exception exception, StringBuilder detailBuilder, bool enableStackTrace) + protected virtual void AddExceptionToDetails(Exception exception, StringBuilder detailBuilder, bool sendStackTraceToClients) { //Exception Message detailBuilder.AppendLine(exception.GetType().Name + ": " + exception.Message); @@ -256,7 +256,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling } //Exception StackTrace - if (enableStackTrace && !string.IsNullOrEmpty(exception.StackTrace)) + if (sendStackTraceToClients && !string.IsNullOrEmpty(exception.StackTrace)) { detailBuilder.AppendLine("STACK TRACE: " + exception.StackTrace); } @@ -264,7 +264,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling //Inner exception if (exception.InnerException != null) { - AddExceptionToDetails(exception.InnerException, detailBuilder, enableStackTrace); + AddExceptionToDetails(exception.InnerException, detailBuilder, sendStackTraceToClients); } //Inner exceptions for AggregateException @@ -278,7 +278,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling foreach (var innerException in aggException.InnerExceptions) { - AddExceptionToDetails(innerException, detailBuilder, enableStackTrace); + AddExceptionToDetails(innerException, detailBuilder, sendStackTraceToClients); } } } @@ -321,7 +321,7 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling return new AbpExceptionHandlingOptions { SendExceptionsDetailsToClients = false, - EnableStackTrace = false + SendStackTraceToClients = true }; } }