Merge commit from fork

* Use Debug Mode to determine content of the ProblemDetails

* Cache the debug value
This commit is contained in:
Bjarke Berg
2024-08-19 15:05:02 +02:00
committed by Nikolaj Geisle
parent 72bef8861d
commit b76070c794
2 changed files with 14 additions and 9 deletions

View File

@@ -33,6 +33,7 @@ internal static class ApplicationBuilderExtensions
{
innerBuilder.UseExceptionHandler(exceptionBuilder => exceptionBuilder.Run(async context =>
{
var isDebug = context.RequestServices.GetRequiredService<IHostingEnvironment>().IsDebugMode;
Exception? exception = context.Features.Get<IExceptionHandlerPathFeature>()?.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;

View File

@@ -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>)
{
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('/') ?? "/";
/// <inheritdoc />
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;
}
}