| | | 1 | | using System.Runtime.ExceptionServices; |
| | | 2 | | using ArturRios.Util.WebApi.Middleware; |
| | | 3 | | using Microsoft.AspNetCore.Diagnostics; |
| | | 4 | | using Microsoft.Extensions.Logging.Abstractions; |
| | | 5 | | |
| | | 6 | | namespace ArturRios.Heimdall.WebApi.Security; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// NFR-28: answers a failure that reaches a developer exception page the decision has turned off |
| | | 10 | | /// with the API's own 500 envelope instead of the page. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// <para> |
| | | 14 | | /// In Development the minimal host adds the developer exception page by itself, inside the |
| | | 15 | | /// startup filters and ahead of the standard pipeline, whatever <see cref="SensitiveDiagnostics" /> |
| | | 16 | | /// says. ExceptionMiddleware sits inside it, so most failures never reach it, but one raised |
| | | 17 | | /// ahead of that middleware (in routing, say) would be shown with its stack trace. The page's |
| | | 18 | | /// exception filters are the hook the framework offers: this one, registered only where the |
| | | 19 | | /// decision keeps the page off, never hands the failure on to the page. |
| | | 20 | | /// </para> |
| | | 21 | | /// <para> |
| | | 22 | | /// The failure is rethrown into a fresh ExceptionMiddleware, so the body is exactly what the |
| | | 23 | | /// standard pipeline writes for any other unhandled exception. The page has already logged |
| | | 24 | | /// the exception, so that middleware logs nothing more. |
| | | 25 | | /// </para> |
| | | 26 | | /// </remarks> |
| | | 27 | | public sealed class SuppressedDeveloperPageFilter : IDeveloperPageExceptionFilter |
| | | 28 | | { |
| | | 29 | | public Task HandleExceptionAsync(ErrorContext errorContext, Func<ErrorContext, Task> next) |
| | | 30 | | { |
| | 1 | 31 | | var failure = ExceptionDispatchInfo.Capture(errorContext.Exception); |
| | | 32 | | |
| | 1 | 33 | | var envelope = new ExceptionMiddleware(_ => |
| | 1 | 34 | | { |
| | 1 | 35 | | failure.Throw(); |
| | 1 | 36 | | |
| | 0 | 37 | | return Task.CompletedTask; |
| | 1 | 38 | | }, NullLogger<ExceptionMiddleware>.Instance); |
| | | 39 | | |
| | 1 | 40 | | return envelope.InvokeAsync(errorContext.HttpContext); |
| | | 41 | | } |
| | | 42 | | } |