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:
parent
eae0d9f913
commit
f93b197d45
12
DigitalData.Core.Exceptions/DependencyInjection.cs
Normal file
12
DigitalData.Core.Exceptions/DependencyInjection.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -6,4 +6,9 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.3.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.5" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
namespace DigitalData.Core.API;
|
namespace DigitalData.Core.Exceptions;
|
||||||
|
|
||||||
using DigitalData.Core.Exceptions;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
@ -14,14 +13,14 @@ using System.Text.Json;
|
|||||||
public class ExceptionHandlingMiddleware
|
public class ExceptionHandlingMiddleware
|
||||||
{
|
{
|
||||||
private readonly RequestDelegate _next;
|
private readonly RequestDelegate _next;
|
||||||
private readonly ILogger<ExceptionHandlingMiddleware> _logger;
|
private readonly ILogger<ExceptionHandlingMiddleware>? _logger;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="ExceptionHandlingMiddleware"/> class.
|
/// Initializes a new instance of the <see cref="ExceptionHandlingMiddleware"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="next">The next middleware in the request pipeline.</param>
|
/// <param name="next">The next middleware in the request pipeline.</param>
|
||||||
/// <param name="logger">The logger instance for logging exceptions.</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;
|
_next = next;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
@ -51,7 +50,7 @@ public class ExceptionHandlingMiddleware
|
|||||||
/// <param name="exception">The exception that occurred.</param>
|
/// <param name="exception">The exception that occurred.</param>
|
||||||
/// <param name="logger">The logger instance for logging the exception.</param>
|
/// <param name="logger">The logger instance for logging the exception.</param>
|
||||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
/// <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";
|
context.Response.ContentType = "application/json";
|
||||||
|
|
||||||
@ -70,7 +69,7 @@ public class ExceptionHandlingMiddleware
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
logger.LogError(exception, "Unhandled exception occurred.");
|
logger?.LogError(exception, "Unhandled exception occurred.");
|
||||||
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
|
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
|
||||||
message = "An unexpected error occurred.";
|
message = "An unexpected error occurred.";
|
||||||
break;
|
break;
|
||||||
Loading…
x
Reference in New Issue
Block a user