From 90e0126d4078b5fef44b085b34caf7a35161ff50 Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 19 Jan 2021 21:12:49 +0200 Subject: Added Custom Middleware for Exception Handling --- .../Extensions/ConfigureCustomMiddleware.cs | 16 +++++++ .../Models/Middleware/ExceptionMiddleware.cs | 50 ++++++++++++++++++++++ src/DevHive.Web/Startup.cs | 3 +- 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs create mode 100644 src/DevHive.Web/Models/Middleware/ExceptionMiddleware.cs (limited to 'src') diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs new file mode 100644 index 0000000..24c2adf --- /dev/null +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs @@ -0,0 +1,16 @@ +using DevHive.Web.Models.Middleware; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class ConfigureCustomMiddleware + { + public static void CustomMiddlewareConfiguration(this IServiceCollection services) { } + + public static void UseCustomMiddlewareConfiguration(this IApplicationBuilder app) + { + app.UseMiddleware(); + } + } +} diff --git a/src/DevHive.Web/Models/Middleware/ExceptionMiddleware.cs b/src/DevHive.Web/Models/Middleware/ExceptionMiddleware.cs new file mode 100644 index 0000000..d952ff4 --- /dev/null +++ b/src/DevHive.Web/Models/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.Models.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 = "Internal Server Error from the custom middleware." + }.ToString()); + } + } +} diff --git a/src/DevHive.Web/Startup.cs b/src/DevHive.Web/Startup.cs index 94aabe8..4e55873 100644 --- a/src/DevHive.Web/Startup.cs +++ b/src/DevHive.Web/Startup.cs @@ -33,6 +33,7 @@ namespace DevHive.Web services.JWTConfiguration(Configuration); services.AutoMapperConfiguration(); services.DependencyInjectionConfiguration(); + services.CustomMiddlewareConfiguration(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -47,7 +48,6 @@ namespace DevHive.Web if (env.IsDevelopment()) { //app.UseDeveloperExceptionPage(); - app.UseExceptionHandler("/api/Error"); app.UseSwaggerConfiguration(); } else @@ -58,6 +58,7 @@ namespace DevHive.Web app.UseDatabaseConfiguration(); app.UseAutoMapperConfiguration(); + app.UseCustomMiddlewareConfiguration(); app.UseEndpoints(endpoints => { -- cgit v1.2.3