public static IApplicationBuilder UseProblemDetailsExceptionHandler(this IApplicationBuilder app, IExceptionProblemDetailsFactory problemDetailsFactory = null) { app.UseExceptionHandler(configure => { configure.Run(async context => { try { var jsonSerializationService = app.ApplicationServices.GetRequiredService <IJsonSerializer>(); if (problemDetailsFactory == null) { problemDetailsFactory = GetDefaultProblemDetailsFactory(); } var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>(); var exception = exceptionHandlerFeature.Error; // Unauthorized if (exception.GetType() == typeof(AuthorizationException)) { context.Response.StatusCode = (int)HttpStatusCode.Forbidden; return; } // NotFound if (exception.GetType() == typeof(ExistenceException)) { context.Response.StatusCode = (int)HttpStatusCode.NotFound; return; } // UnprocessableEntity var problemDetails = problemDetailsFactory.Create(exception); if (problemDetails != null) { var instance = problemDetails.Instance; context.Response.StatusCode = problemDetails.Status.Value; await context.Response.WriteJsonAsync(problemDetails, jsonSerializationService); } } catch (Exception) { // TODO: VC: Attempt log throw; } }); }); return(app); }
public static IApplicationBuilder UseProblemDetailsExceptionHandler(this IApplicationBuilder app, IExceptionProblemDetailsFactory problemDetailsFactory = null) { app.UseExceptionHandler(configure => { configure.Run(async context => { try { var exceptionHandler = app.ApplicationServices.GetRequiredService <IExceptionHandler>(); var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>(); var exception = exceptionHandlerFeature.Error; await exceptionHandler.HandleAsync(context, exception); } catch (Exception) { throw; } }); }); return(app); }