From f93b197d45a001958cbcd8a8b3ec7c230f79d9e5 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Fri, 16 May 2025 15:55:27 +0200 Subject: [PATCH] Refactor ExceptionHandlingMiddleware and update dependencies Rewrote the `ExceptionHandlingMiddleware` class to improve structure and functionality, changing its namespace to `DigitalData.Core.Exceptions`. Updated the constructor to support a nullable logger and implemented null-conditional logging for unhandled exceptions. Added new package references in `DigitalData.Core.Exceptions.csproj` for `Microsoft.AspNetCore.Http.Abstractions` and `Microsoft.Extensions.Logging.Abstractions`. Introduced a new `DependencyInjection` class to register the middleware in the application's pipeline. --- DigitalData.Core.Exceptions/DependencyInjection.cs | 12 ++++++++++++ .../DigitalData.Core.Exceptions.csproj | 5 +++++ .../ExceptionHandlingMiddleware.cs | 11 +++++------ 3 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 DigitalData.Core.Exceptions/DependencyInjection.cs rename {DigitalData.Core.API => DigitalData.Core.Exceptions}/ExceptionHandlingMiddleware.cs (90%) diff --git a/DigitalData.Core.Exceptions/DependencyInjection.cs b/DigitalData.Core.Exceptions/DependencyInjection.cs new file mode 100644 index 0000000..ec40485 --- /dev/null +++ b/DigitalData.Core.Exceptions/DependencyInjection.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Builder; + +namespace DigitalData.Core.Exceptions; + +public static class DependencyInjection +{ + public static IApplicationBuilder UseExceptionHandlingMiddleware(this IApplicationBuilder app) + { + app.UseMiddleware(); + return app; + } +} diff --git a/DigitalData.Core.Exceptions/DigitalData.Core.Exceptions.csproj b/DigitalData.Core.Exceptions/DigitalData.Core.Exceptions.csproj index bae5b9f..0f74356 100644 --- a/DigitalData.Core.Exceptions/DigitalData.Core.Exceptions.csproj +++ b/DigitalData.Core.Exceptions/DigitalData.Core.Exceptions.csproj @@ -6,4 +6,9 @@ enable + + + + + diff --git a/DigitalData.Core.API/ExceptionHandlingMiddleware.cs b/DigitalData.Core.Exceptions/ExceptionHandlingMiddleware.cs similarity index 90% rename from DigitalData.Core.API/ExceptionHandlingMiddleware.cs rename to DigitalData.Core.Exceptions/ExceptionHandlingMiddleware.cs index b02655d..2963c79 100644 --- a/DigitalData.Core.API/ExceptionHandlingMiddleware.cs +++ b/DigitalData.Core.Exceptions/ExceptionHandlingMiddleware.cs @@ -1,6 +1,5 @@ -namespace DigitalData.Core.API; +namespace DigitalData.Core.Exceptions; -using DigitalData.Core.Exceptions; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using System.Net; @@ -14,14 +13,14 @@ using System.Text.Json; public class ExceptionHandlingMiddleware { private readonly RequestDelegate _next; - private readonly ILogger _logger; + private readonly ILogger? _logger; /// /// Initializes a new instance of the class. /// /// The next middleware in the request pipeline. /// The logger instance for logging exceptions. - public ExceptionHandlingMiddleware(RequestDelegate next, ILogger logger) + public ExceptionHandlingMiddleware(RequestDelegate next, ILogger? logger = null) { _next = next; _logger = logger; @@ -51,7 +50,7 @@ public class ExceptionHandlingMiddleware /// The exception that occurred. /// The logger instance for logging the exception. /// A task that represents the asynchronous operation. - private static async Task HandleExceptionAsync(HttpContext context, Exception exception, ILogger logger) + private static async Task HandleExceptionAsync(HttpContext context, Exception exception, ILogger? logger = null) { context.Response.ContentType = "application/json"; @@ -70,7 +69,7 @@ public class ExceptionHandlingMiddleware break; default: - logger.LogError(exception, "Unhandled exception occurred."); + logger?.LogError(exception, "Unhandled exception occurred."); context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; message = "An unexpected error occurred."; break;