aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Web/Controllers/ErrorController.cs
blob: c3f1e55b7e2b1dab43a932169c76f6992ec796e9 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Diagnostics;
using AutoMapper;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace DevHive.Web.Controllers
{
	public class ErrorController : ControllerBase
	{
		[HttpPost]
		[Route("/api/Error")]
		public IActionResult Error()
		{
			//Later for logging
			string requestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;

			IExceptionHandlerFeature exception =
			HttpContext.Features.Get<IExceptionHandlerFeature>();
		
			object result = ProcessException(requestId, exception);
			return new BadRequestObjectResult(JsonConvert.SerializeObject(result));
		}

		private object ProcessException(string requestId, IExceptionHandlerFeature exception)
		{
			switch (exception.Error)
			{
				case ArgumentException _:
				case InvalidOperationException _:
				case AutoMapperMappingException _:
				case AutoMapperConfigurationException _:
					return MessageToObject(exception.Error.Message);
				default:
					return MessageToObject(null);
			}
		}

		private object MessageToObject(string message)
		{
			return new
			{
				Error = message
			};
		}
	}
}