diff options
| author | transtrike <transtrike@gmail.com> | 2021-04-08 14:20:37 +0300 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-04-08 14:20:37 +0300 |
| commit | ea4dafaf8f41cbcce19fa4d872d9ed22590db7d5 (patch) | |
| tree | bf40be440007a20261b436c9f7259fa244848f65 /src/Web/DevHive.Web | |
| parent | ffc6b7cd6e454627c95044e88037b37d31dcfce3 (diff) | |
| download | DevHive-ea4dafaf8f41cbcce19fa4d872d9ed22590db7d5.tar DevHive-ea4dafaf8f41cbcce19fa4d872d9ed22590db7d5.tar.gz DevHive-ea4dafaf8f41cbcce19fa4d872d9ed22590db7d5.zip | |
Improved parsing of errors from Attributes. Reverted old style of errors
shown
Diffstat (limited to 'src/Web/DevHive.Web')
3 files changed, 28 insertions, 2 deletions
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<ApiBehaviorOptions>(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<ExceptionMiddleware>(); 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. |
