aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Web/Middleware/ExceptionMiddleware.cs
diff options
context:
space:
mode:
authortranstrike <transtrike@gmail.com>2021-01-21 22:13:16 +0200
committertranstrike <transtrike@gmail.com>2021-01-21 22:13:16 +0200
commit13a2ceda912f961a232c87236f1b29aa29bb6160 (patch)
tree59f8d2bf63b03bacc76f98114d2aed78e420ddcd /src/DevHive.Web/Middleware/ExceptionMiddleware.cs
parenta47ea20ab91017da53437f750ed8e0f939f5cdba (diff)
parentbda98b96433d7a9952524fab4ec65f96998b55de (diff)
downloadDevHive-13a2ceda912f961a232c87236f1b29aa29bb6160.tar
DevHive-13a2ceda912f961a232c87236f1b29aa29bb6160.tar.gz
DevHive-13a2ceda912f961a232c87236f1b29aa29bb6160.zip
Merge branch 'refactor_user_updating' into dev
Diffstat (limited to 'src/DevHive.Web/Middleware/ExceptionMiddleware.cs')
-rw-r--r--src/DevHive.Web/Middleware/ExceptionMiddleware.cs50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/DevHive.Web/Middleware/ExceptionMiddleware.cs b/src/DevHive.Web/Middleware/ExceptionMiddleware.cs
new file mode 100644
index 0000000..cb6d4ca
--- /dev/null
+++ b/src/DevHive.Web/Middleware/ExceptionMiddleware.cs
@@ -0,0 +1,50 @@
+using System;
+using System.Net;
+using System.Threading.Tasks;
+using Microsoft.AspNetCore.Http;
+using Microsoft.Extensions.Logging;
+
+namespace DevHive.Web.Middleware
+{
+ public class ExceptionMiddleware
+ {
+ private readonly RequestDelegate _next;
+ // private readonly ILogger _logger;
+
+ public ExceptionMiddleware(RequestDelegate next)
+ {
+ this._next = next;
+ // this._logger = logger;
+ }
+ // public ExceptionMiddleware(RequestDelegate next, ILogger logger)
+ // {
+ // this._logger = logger;
+ // this._next = next;
+ // }
+
+ public async Task InvokeAsync(HttpContext httpContext)
+ {
+ try
+ {
+ await this._next(httpContext);
+ }
+ catch (Exception ex)
+ {
+ // this._logger.LogError($"Something went wrong: {ex}");
+ await HandleExceptionAsync(httpContext, ex);
+ }
+ }
+
+ private Task HandleExceptionAsync(HttpContext context, Exception exception)
+ {
+ context.Response.ContentType = "application/json";
+ context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
+
+ return context.Response.WriteAsync(new
+ {
+ StatusCode = context.Response.StatusCode,
+ Message = exception.Message
+ }.ToString());
+ }
+ }
+}