Fix for UseExceptionHandler no longer working since v10.3 RC (#13218)

* Fix for UseExceptionHandler no longer working since v10.3 RC

* Update the management api path to match the new one

Co-authored-by: Nikolaj <nikolajlauridsen@protonmail.ch>
This commit is contained in:
Justin Neville
2022-10-26 12:56:09 +01:00
committed by GitHub
parent 2f57e84aba
commit f5aba340e4

View File

@@ -84,24 +84,37 @@ public class ManagementApiComposer : IComposer
"BackofficeSwagger",
applicationBuilder =>
{
applicationBuilder.UseExceptionHandler(exceptionBuilder => exceptionBuilder.Run(async context =>
{
Exception? exception = context.Features.Get<IExceptionHandlerPathFeature>()?.Error;
if (exception is null)
// Only use the API exception handler when we are requesting an API
applicationBuilder.UseWhen(
httpContext =>
{
return;
}
GlobalSettings? settings = httpContext.RequestServices.GetRequiredService<IOptions<GlobalSettings>>().Value;
IHostingEnvironment hostingEnvironment = httpContext.RequestServices.GetRequiredService<IHostingEnvironment>();
var officePath = settings.GetBackOfficePath(hostingEnvironment);
var response = new ProblemDetails
return httpContext.Request.Path.Value?.StartsWith($"{officePath}/management/api/") ?? false;
},
innerBuilder =>
{
Title = exception.Message,
Detail = exception.StackTrace,
Status = StatusCodes.Status500InternalServerError,
Instance = exception.GetType().Name,
Type = "Error",
};
await context.Response.WriteAsJsonAsync(response);
}));
innerBuilder.UseExceptionHandler(exceptionBuilder => exceptionBuilder.Run(async context =>
{
Exception? exception = context.Features.Get<IExceptionHandlerPathFeature>()?.Error;
if (exception is null)
{
return;
}
var response = new ProblemDetails
{
Title = exception.Message,
Detail = exception.StackTrace,
Status = StatusCodes.Status500InternalServerError,
Instance = exception.GetType().Name,
Type = "Error",
};
await context.Response.WriteAsJsonAsync(response);
}));
});
},
applicationBuilder =>
{