aboutsummaryrefslogtreecommitdiff
path: root/src/Web/DevHive.Web/Middleware
diff options
context:
space:
mode:
Diffstat (limited to 'src/Web/DevHive.Web/Middleware')
-rw-r--r--src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs b/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs
new file mode 100644
index 0000000..680d824
--- /dev/null
+++ b/src/Web/DevHive.Web/Middleware/ExceptionMiddleware.cs
@@ -0,0 +1,44 @@
+using System;
+using System.Net;
+using System.Threading.Tasks;
+using Microsoft.AspNetCore.Http;
+using Newtonsoft.Json;
+
+namespace DevHive.Web.Middleware
+{
+ public class ExceptionMiddleware
+ {
+ private readonly RequestDelegate _next;
+
+ public ExceptionMiddleware(RequestDelegate next)
+ {
+ this._next = next;
+ }
+
+ public async Task InvokeAsync(HttpContext httpContext)
+ {
+ try
+ {
+ await this._next(httpContext);
+ }
+ catch (Exception ex)
+ {
+ await HandleExceptionAsync(httpContext, ex);
+ }
+ }
+
+ private static Task HandleExceptionAsync(HttpContext context, Exception exception)
+ {
+ context.Response.ContentType = "application/json";
+ context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
+
+ var problems = new
+ {
+ errors = new { Exception = new String[] { exception.Message } }
+ };
+
+ string message = JsonConvert.SerializeObject(problems);
+ return context.Response.WriteAsync(message);
+ }
+ }
+}