From ea4dafaf8f41cbcce19fa4d872d9ed22590db7d5 Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 8 Apr 2021 14:20:37 +0300 Subject: Improved parsing of errors from Attributes. Reverted old style of errors shown --- .../ConfigureExceptionHandlerMiddleware.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/Web/DevHive.Web/Configurations') diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs index c017a8c..6885f84 100644 --- a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs +++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs @@ -1,11 +1,33 @@ using DevHive.Web.Middleware; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Configuration; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; namespace DevHive.Web.Configurations.Extensions { public static class ConfigureExceptionHandlerMiddleware { + public static void ConfigureExceptionHandler(this IServiceCollection services, IConfiguration configuration) + { + services.Configure(o => + { + o.InvalidModelStateResponseFactory = actionContext => + { + var problemDetails = new ValidationProblemDetails(actionContext.ModelState) + { + Status = StatusCodes.Status422UnprocessableEntity + }; + + return new UnprocessableEntityObjectResult(problemDetails) + { + ContentTypes = { "application/problem+json" } + }; + }; + }); + } + public static void UseExceptionHandlerMiddlewareConfiguration(this IApplicationBuilder app) { app.UseMiddleware(); -- cgit v1.2.3