/* This file is part of NKK. NKK is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. NKK is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with NKK. If not, see . */ using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Http.Features; using Serilog; using ILogger = Serilog.ILogger; namespace NKK; public class ExceptionCatchingMiddleware(RequestDelegate next) { private readonly ILogger _logger = Log.Logger.ForContext(); public static String Name_ItemTriggered = "nhnd_ExceptionMiddlewareTriggered"; public async Task InvokeAsync(HttpContext context) { if (context.Features.Get() != null) { await next(context); return; } try { await next(context); } catch (Exception e) { var oMethod = context.Request.Method; var oPath = context.Request.Path; var oQuery = context.Request.QueryString; var oEndpoint = context.GetEndpoint(); var oRouteValues = context.Features.Get()?.RouteValues; var oRequestServices = context.RequestServices; if (e is StatusCodeException se) context.Response.StatusCode = se.StatusCode; else context.Response.StatusCode = 500; context.Features.Set(new ExceptionHandlerFeature { Error = e, Path = oPath, Endpoint = oEndpoint, RouteValues = oRouteValues, }); context.Features.Set(new StatusCodeReExecuteFeature { OriginalPath = oPath, OriginalQueryString = oQuery.Value, Endpoint = oEndpoint, RouteValues = oRouteValues, }); context.SetEndpoint(null); context.Features.Set(null); context.Request.Method = HttpMethod.Get.ToString(); context.Request.Path = new PathString("/Error"); // TODO configurable context.Request.QueryString = QueryString.Empty; context.Items[Name_ItemTriggered] = true; var scopeFactory = oRequestServices.GetRequiredService(); await using var scope = scopeFactory.CreateAsyncScope(); context.RequestServices = scope.ServiceProvider; try { await next(context); } catch (Exception ex) { this._logger.Error(ex, "Rendering the Error page threw an exception!"); } finally { context.Request.Method = oMethod; context.Request.Path = oPath; context.Request.QueryString = oQuery; context.Features.Set(null); context.RequestServices = oRequestServices; } } } } /// /// Exception class to trigger an error page anywhere /// public class StatusCodeException : Exception { public readonly int StatusCode; /// /// Error status /// /// /// Thrown if does not indicate an error (not 400 <= statusCode < 600); /// public StatusCodeException(int statusCode) { if (statusCode < 400 || statusCode >= 600) throw new ArgumentOutOfRangeException(nameof(statusCode)); this.StatusCode = statusCode; } }