diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs | 16 | ||||
| -rw-r--r-- | src/DevHive.Web/Models/Middleware/ExceptionMiddleware.cs | 50 | ||||
| -rw-r--r-- | src/DevHive.Web/Startup.cs | 3 |
3 files changed, 68 insertions, 1 deletions
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<ExceptionMiddleware>(); + } + } +} 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 => { |
