diff options
| author | transtrike <transtrike@gmail.com> | 2021-01-19 21:46:06 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-01-19 21:46:06 +0200 |
| commit | 8560caa13b7f7d0e80ee712c77efc59a80e8935c (patch) | |
| tree | 02643afdd46122dfd904fbb8632117a5494081a8 /src/DevHive.Web/Middleware/ExceptionMiddleware.cs | |
| parent | 661c3194b750e42146e9e28a33da08419b2b2cea (diff) | |
| download | DevHive-8560caa13b7f7d0e80ee712c77efc59a80e8935c.tar DevHive-8560caa13b7f7d0e80ee712c77efc59a80e8935c.tar.gz DevHive-8560caa13b7f7d0e80ee712c77efc59a80e8935c.zip | |
Added attributes to the Web models for validations
Diffstat (limited to 'src/DevHive.Web/Middleware/ExceptionMiddleware.cs')
| -rw-r--r-- | src/DevHive.Web/Middleware/ExceptionMiddleware.cs | 50 |
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()); + } + } +} |
