aboutsummaryrefslogtreecommitdiff
path: root/src/Web/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs
blob: 6885f84621eaf989ec46e7e0bbe7c3961090df29 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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>();
		}
	}
}