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.
This commit is contained in:
Developer 02 2025-05-16 15:55:27 +02:00
parent eae0d9f913
commit f93b197d45
3 changed files with 22 additions and 6 deletions

View File

@ -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<ExceptionHandlingMiddleware>();
return app;
}
}

View File

@ -6,4 +6,9 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.3.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.5" />
</ItemGroup>
</Project>

View File

@ -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<ExceptionHandlingMiddleware> _logger;
private readonly ILogger<ExceptionHandlingMiddleware>? _logger;
/// <summary>
/// Initializes a new instance of the <see cref="ExceptionHandlingMiddleware"/> class.
/// </summary>
/// <param name="next">The next middleware in the request pipeline.</param>
/// <param name="logger">The logger instance for logging exceptions.</param>
public ExceptionHandlingMiddleware(RequestDelegate next, ILogger<ExceptionHandlingMiddleware> logger)
public ExceptionHandlingMiddleware(RequestDelegate next, ILogger<ExceptionHandlingMiddleware>? logger = null)
{
_next = next;
_logger = logger;
@ -51,7 +50,7 @@ public class ExceptionHandlingMiddleware
/// <param name="exception">The exception that occurred.</param>
/// <param name="logger">The logger instance for logging the exception.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
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;