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 ++++++++++++++++++++++ .../DevHive.Web/Middleware/ExceptionMiddleware.cs | 7 +++++-- src/Web/DevHive.Web/Startup.cs | 1 + 3 files changed, 28 insertions(+), 2 deletions(-) (limited to 'src/Web') 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(); diff --git a/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs b/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs index ebec5e8..680d824 100644 --- a/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs +++ b/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs @@ -32,9 +32,12 @@ namespace DevHive.Web.Middleware context.Response.ContentType = "application/json"; context.Response.StatusCode = (int)HttpStatusCode.BadRequest; - // Made to resemble the formatting of property validation errors (like [MinLength(3)]) - string message = JsonConvert.SerializeObject(new { Error = exception.Message }); + var problems = new + { + errors = new { Exception = new String[] { exception.Message } } + }; + string message = JsonConvert.SerializeObject(problems); return context.Response.WriteAsync(message); } } diff --git a/src/Web/DevHive.Web/Startup.cs b/src/Web/DevHive.Web/Startup.cs index 05a75d9..49fa408 100644 --- a/src/Web/DevHive.Web/Startup.cs +++ b/src/Web/DevHive.Web/Startup.cs @@ -34,6 +34,7 @@ namespace DevHive.Web services.SwaggerConfiguration(); services.JWTConfiguration(Configuration); services.AutoMapperConfiguration(); + services.ConfigureExceptionHandler(this.Configuration); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. -- cgit v1.2.3