Add global exception handling middleware
Introduces `GlobalExceptionHandlerMiddleware` for managing exceptions in the request pipeline, logging them, and returning JSON error responses. A new project, `DigitalData.Core.Exceptions.Middleware`, is created to house this middleware and related classes. The `GlobalExceptionHandlerOptions` class allows for custom exception registration with specific HTTP status codes, while a new `HttpResponse` record encapsulates status codes and messages for structured responses. The middleware is registered in the `DependencyInjection` class for easy integration.
This commit is contained in:
parent
50c19fea31
commit
83ba492b37
@ -0,0 +1,12 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
|
||||
namespace DigitalData.Core.Exceptions.Middleware;
|
||||
|
||||
public static class DependencyInjection
|
||||
{
|
||||
public static IApplicationBuilder UseGlobalExceptionHandler(this IApplicationBuilder app)
|
||||
{
|
||||
app.UseMiddleware<GlobalExceptionHandlerMiddleware>();
|
||||
return app;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net7.0;net8.0;net9.0</TargetFrameworks>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DigitalData.Core.Exceptions\DigitalData.Core.Exceptions.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@ -1,4 +1,4 @@
|
||||
namespace DigitalData.Core.Exceptions;
|
||||
namespace DigitalData.Core.Exceptions.Middleware;
|
||||
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@ -0,0 +1,14 @@
|
||||
using System.Net;
|
||||
|
||||
namespace DigitalData.Core.Exceptions.Middleware;
|
||||
|
||||
public class GlobalExceptionHandlerOptions
|
||||
{
|
||||
private readonly Dictionary<Type, HttpResponse> _registeredExceptions = new();
|
||||
|
||||
public GlobalExceptionHandlerOptions RegisterException<TException>(HttpStatusCode statusCode, string? message = null) where TException : Exception
|
||||
{
|
||||
_registeredExceptions[typeof(TException)] = new HttpResponse(statusCode, message);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
5
DigitalData.Core.Exceptions.Middleware/HttpResponse.cs
Normal file
5
DigitalData.Core.Exceptions.Middleware/HttpResponse.cs
Normal file
@ -0,0 +1,5 @@
|
||||
using System.Net;
|
||||
|
||||
namespace DigitalData.Core.Exceptions.Middleware;
|
||||
|
||||
public record HttpResponse(HttpStatusCode StatusCode, string? Message = null);
|
||||
@ -41,6 +41,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Infrastructure", "Infrastru
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalData.Core.Exceptions", "DigitalData.Core.Exceptions\DigitalData.Core.Exceptions.csproj", "{BAEF6CC9-4FE2-4E3F-9D32-911C9E8CCFB4}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalData.Core.Exceptions.Middleware", "DigitalData.Core.Exceptions.Middleware\DigitalData.Core.Exceptions.Middleware.csproj", "{2336AE61-A21D-437E-A11B-367D008A64B2}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Exceptions", "Exceptions", "{8C3AF25D-81D9-4651-90CA-BF0BD2A03EA7}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -106,6 +110,10 @@ Global
|
||||
{BAEF6CC9-4FE2-4E3F-9D32-911C9E8CCFB4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{BAEF6CC9-4FE2-4E3F-9D32-911C9E8CCFB4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{BAEF6CC9-4FE2-4E3F-9D32-911C9E8CCFB4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{2336AE61-A21D-437E-A11B-367D008A64B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{2336AE61-A21D-437E-A11B-367D008A64B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2336AE61-A21D-437E-A11B-367D008A64B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{2336AE61-A21D-437E-A11B-367D008A64B2}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
@ -127,7 +135,9 @@ Global
|
||||
{C9266749-9504-4EA9-938F-F083357B60B7} = {72CBAFBA-55CC-49C9-A484-F8F4550054CB}
|
||||
{CE00E1F7-2771-4D9C-88FB-E564894E539E} = {41795B74-A757-4E93-B907-83BFF04EEE5C}
|
||||
{41795B74-A757-4E93-B907-83BFF04EEE5C} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
|
||||
{BAEF6CC9-4FE2-4E3F-9D32-911C9E8CCFB4} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
|
||||
{BAEF6CC9-4FE2-4E3F-9D32-911C9E8CCFB4} = {8C3AF25D-81D9-4651-90CA-BF0BD2A03EA7}
|
||||
{2336AE61-A21D-437E-A11B-367D008A64B2} = {8C3AF25D-81D9-4651-90CA-BF0BD2A03EA7}
|
||||
{8C3AF25D-81D9-4651-90CA-BF0BD2A03EA7} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {8E2C3187-F848-493A-9E79-56D20DDCAC94}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user