static async Task Awaited(EndpointExceptionMappingMiddleware middleware, Task task, HttpContext httpContext) { ExceptionDispatchInfo?edi2 = null; try { await task; } catch (Exception ex) { edi2 = ExceptionDispatchInfo.Capture(ex); } if (edi2 != null) { PrepareExceptionMappingContext(middleware, httpContext); edi2.Throw(); } }
/// <summary> /// Injects exception mapper into exception handler middleware and wraps the current request /// pipeline into the new pipeline. /// </summary> static RequestDelegate BuildExceptionMappingPipeline( EndpointExceptionMappingMiddleware middleware, IApplicationBuilder app, RequestDelegate next) { var exceptionHandlerOptions = new ExceptionHandlerOptions { ExceptionHandler = middleware.OnExceptionHandler }; return // Wrap everything with exception handler middleware with exception mapping injected (app.UseExceptionHandler(exceptionHandlerOptions) // Use a middleware to capture exception mapping context when exception occurred // This is needed because exception handler middleware clears required endpoint // information before calling the handler delegate .Use(middleware.CaptureExceptionMappingContext) // Call original request pipeline in wrapped pipeline .Use(_ => next) .Build()); }