From b76070c794925932cb159ef50b851db6e966a004 Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Mon, 19 Aug 2024 15:05:02 +0200 Subject: [PATCH] Merge commit from fork * Use Debug Mode to determine content of the ProblemDetails * Cache the debug value --- .../ApplicationBuilderExtensions.cs | 7 ++++--- .../AspNetCore/AspNetCoreHostingEnvironment.cs | 16 ++++++++++------ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Umbraco.Cms.Api.Management/DependencyInjection/ApplicationBuilderExtensions.cs b/src/Umbraco.Cms.Api.Management/DependencyInjection/ApplicationBuilderExtensions.cs index 9018b97bee..870c4d3e1e 100644 --- a/src/Umbraco.Cms.Api.Management/DependencyInjection/ApplicationBuilderExtensions.cs +++ b/src/Umbraco.Cms.Api.Management/DependencyInjection/ApplicationBuilderExtensions.cs @@ -33,6 +33,7 @@ internal static class ApplicationBuilderExtensions { innerBuilder.UseExceptionHandler(exceptionBuilder => exceptionBuilder.Run(async context => { + var isDebug = context.RequestServices.GetRequiredService().IsDebugMode; Exception? exception = context.Features.Get()?.Error; if (exception is null) { @@ -42,16 +43,16 @@ internal static class ApplicationBuilderExtensions var response = new ProblemDetails { Title = exception.Message, - Detail = exception.StackTrace, + Detail = isDebug ? exception.StackTrace : null, Status = StatusCodes.Status500InternalServerError, - Instance = exception.GetType().Name, + Instance = isDebug ? exception.GetType().Name : null, Type = "Error" }; await context.Response.WriteAsJsonAsync(response); })); }); - internal static IApplicationBuilder UseEndpoints(this IApplicationBuilder applicationBuilder) +internal static IApplicationBuilder UseEndpoints(this IApplicationBuilder applicationBuilder) { IServiceProvider provider = applicationBuilder.ApplicationServices; diff --git a/src/Umbraco.Web.Common/AspNetCore/AspNetCoreHostingEnvironment.cs b/src/Umbraco.Web.Common/AspNetCore/AspNetCoreHostingEnvironment.cs index 8d471428de..324781b5a3 100644 --- a/src/Umbraco.Web.Common/AspNetCore/AspNetCoreHostingEnvironment.cs +++ b/src/Umbraco.Web.Common/AspNetCore/AspNetCoreHostingEnvironment.cs @@ -43,14 +43,14 @@ public class AspNetCoreHostingEnvironment : IHostingEnvironment _webHostEnvironment = webHostEnvironment ?? throw new ArgumentNullException(nameof(webHostEnvironment)); _urlProviderMode = _webRoutingSettings.CurrentValue.UrlProviderMode; - SetSiteName(hostingSettings.CurrentValue.SiteName); + SetSiteNameAndDebugMode(hostingSettings.CurrentValue); // We have to ensure that the OptionsMonitor is an actual options monitor since we have a hack // where we initially use an OptionsMonitorAdapter, which doesn't implement OnChange. // See summery of OptionsMonitorAdapter for more information. if (hostingSettings is OptionsMonitor) { - hostingSettings.OnChange(settings => SetSiteName(settings.SiteName)); + hostingSettings.OnChange(settings => SetSiteNameAndDebugMode(settings)); } ApplicationPhysicalPath = webHostEnvironment.ContentRootPath; @@ -95,7 +95,7 @@ public class AspNetCoreHostingEnvironment : IHostingEnvironment _hostingSettings.CurrentValue.ApplicationVirtualPath?.EnsureStartsWith('/') ?? "/"; /// - public bool IsDebugMode => _hostingSettings.CurrentValue.Debug; + public bool IsDebugMode { get; private set; } public string LocalTempPath { @@ -188,8 +188,12 @@ public class AspNetCoreHostingEnvironment : IHostingEnvironment } } - private void SetSiteName(string? siteName) => - SiteName = string.IsNullOrWhiteSpace(siteName) + private void SetSiteNameAndDebugMode(HostingSettings hostingSettings) + { + SiteName = string.IsNullOrWhiteSpace(hostingSettings.SiteName) ? _webHostEnvironment.ApplicationName - : siteName; + : hostingSettings.SiteName; + + IsDebugMode = hostingSettings.Debug; + } }