aboutsummaryrefslogtreecommitdiff
path: root/src/Web/DevHive.Web
diff options
context:
space:
mode:
Diffstat (limited to 'src/Web/DevHive.Web')
-rw-r--r--src/Web/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs22
-rw-r--r--src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs7
-rw-r--r--src/Web/DevHive.Web/Startup.cs1
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.