internal static IApplicationBuilder UseApiExceptionHandler( this IApplicationBuilder app, string corsPolicyName, StartupUseOptions startupOptions) => UseApiExceptionHandling( app, corsPolicyName, startupOptions);
internal static IApplicationBuilder UseApiExceptionHandler( this IApplicationBuilder app, string corsPolicyName, StartupUseOptions startupUseOptions) => UseApiExceptionHandling( app, corsPolicyName, startupUseOptions, app.ApplicationServices.GetRequiredService <ProblemDetailsHelper>());
private static IApplicationBuilder UseApiExceptionHandling( IApplicationBuilder app, string corsPolicyName, StartupUseOptions startupOptions) { _logger = startupOptions.Common.LoggerFactory.CreateLogger <ApiExceptionHandler>(); var customHandlers = startupOptions.Api.CustomExceptionHandlers ?? new IExceptionHandler[] { }; var exceptionHandler = new ExceptionHandler(_logger, customHandlers); app.UseExceptionHandler(builder => { builder.UseCors(policyName: corsPolicyName); startupOptions.MiddlewareHooks.AfterCors?.Invoke(builder); builder.UseProblemDetails(); startupOptions.MiddlewareHooks.AfterProblemDetails?.Invoke(builder); builder .UseMiddleware <EnableRequestRewindMiddleware>() // https://github.com/serilog/serilog-aspnetcore/issues/59 .UseMiddleware <AddCorrelationIdToResponseMiddleware>() .UseMiddleware <AddCorrelationIdMiddleware>() .UseMiddleware <AddCorrelationIdToLogContextMiddleware>() .UseMiddleware <AddHttpSecurityHeadersMiddleware>(startupOptions.Server.ServerName, startupOptions.Server.PoweredByName) .UseMiddleware <AddRemoteIpAddressMiddleware>(startupOptions.Api.RemoteIpAddressClaimName) .UseMiddleware <AddVersionHeaderMiddleware>(startupOptions.Server.VersionHeaderName); startupOptions.MiddlewareHooks.AfterMiddleware?.Invoke(builder); builder .UseMiddleware <DefaultResponseCompressionQualityMiddleware>(new Dictionary <string, double> { { "br", 1.0 }, { "gzip", 0.9 } }) .UseResponseCompression(); startupOptions.MiddlewareHooks.AfterResponseCompression?.Invoke(builder); var requestLocalizationOptions = startupOptions .Common .ServiceProvider .GetRequiredService <IOptions <RequestLocalizationOptions> >() .Value; builder.UseRequestLocalization(requestLocalizationOptions); startupOptions.MiddlewareHooks.AfterRequestLocalization?.Invoke(builder); builder .Run(async context => { context.Response.StatusCode = StatusCodes.Status500InternalServerError; context.Response.ContentType = MediaTypeNames.Application.Json; var error = context.Features.Get <IExceptionHandlerFeature>(); var exception = error?.Error; // Errors happening in the Apply() stuff result in an InvocationException due to the dynamic stuff. if (exception is TargetInvocationException && exception.InnerException != null) { exception = exception.InnerException; } await exceptionHandler.HandleException(exception, context); }); }); return(app); }