Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit d8f6f5e

Browse files
committed
Aggiunta configurazione Exception Middleware
1 parent da51146 commit d8f6f5e

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace NET6CustomLibrary.ExceptionsMiddleware;
2+
3+
internal class ExceptionResult
4+
{
5+
public HttpStatusCode ErrorCode { get; set; }
6+
public string ErrorStatus { get; set; }
7+
public string ErrorMessage { get; set; }
8+
public string ErrorDetails { get; set; }
9+
//public string StackTrace { get; set; }
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace NET6CustomLibrary.ExceptionsMiddleware.Exceptions;
2+
3+
public class BadRequestException : Exception
4+
{
5+
public BadRequestException(string message) : base(message)
6+
{
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace NET6CustomLibrary.ExceptionsMiddleware.Exceptions;
2+
3+
public class NotFoundException : Exception
4+
{
5+
public NotFoundException(string message) : base(message)
6+
{
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
namespace NET6CustomLibrary.ExceptionsMiddleware;
2+
3+
public class GlobalErrorHandlingMiddleware
4+
{
5+
private readonly RequestDelegate next;
6+
7+
public GlobalErrorHandlingMiddleware(RequestDelegate next)
8+
{
9+
this.next = next;
10+
}
11+
12+
public async Task InvokeAsync(HttpContext context)
13+
{
14+
try
15+
{
16+
await next(context);
17+
}
18+
catch (Exception ex)
19+
{
20+
await HandleExceptionAsync(context, ex);
21+
}
22+
}
23+
24+
private static Task HandleExceptionAsync(HttpContext context, Exception exception)
25+
{
26+
var exceptionType = exception.GetType();
27+
28+
HttpStatusCode status;
29+
HttpStatusCode errorCode;
30+
31+
string errorStatus;
32+
string errorDetails;
33+
string errorMessage;
34+
//string stackTrace;
35+
36+
if (exceptionType == typeof(BadRequestException))
37+
{
38+
status = HttpStatusCode.BadRequest;
39+
errorCode = status;
40+
errorDetails = $"https://httpstatuses.io/{(int)status}";
41+
errorStatus = "BadRequest";
42+
errorMessage = exception.Message;
43+
//stackTrace = exception.StackTrace;
44+
}
45+
else if (exceptionType == typeof(NotFoundException))
46+
{
47+
status = HttpStatusCode.NotFound;
48+
errorCode = status;
49+
errorDetails = $"https://httpstatuses.io/{(int)status}";
50+
errorStatus = "NotFound";
51+
errorMessage = exception.Message;
52+
//stackTrace = exception.StackTrace;
53+
}
54+
else
55+
{
56+
status = HttpStatusCode.InternalServerError;
57+
errorCode = status;
58+
errorDetails = $"https://httpstatuses.io/{(int)status}";
59+
errorStatus = "InternalServerError";
60+
errorMessage = exception.Message;
61+
//stackTrace = exception.StackTrace;
62+
}
63+
64+
var exceptionResult = JsonSerializer.Serialize(new ExceptionResult()
65+
{
66+
ErrorStatus = errorStatus,
67+
ErrorCode = errorCode,
68+
ErrorDetails = errorDetails,
69+
ErrorMessage = errorMessage
70+
//ErrorMessage = errorMessage,
71+
//StackTrace = stackTrace
72+
});
73+
74+
context.Response.ContentType = "application/json";
75+
context.Response.StatusCode = (int)status;
76+
77+
return context.Response.WriteAsync(exceptionResult);
78+
}
79+
}

src/NET6CustomLibrary/Extensions/DependencyInjection.cs

+4
Original file line numberDiff line numberDiff line change
@@ -494,4 +494,8 @@ public static IServiceCollection AddFullPolicyCors(this IServiceCollection servi
494494
return services;
495495
}
496496
#endregion
497+
498+
//Rif: https://dev.to/moe23/net-6-web-api-global-exceptions-handling-1a46
499+
public static IApplicationBuilder AddGlobalErrorHandler(this IApplicationBuilder applicationBuilder)
500+
=> applicationBuilder.UseMiddleware<GlobalErrorHandlingMiddleware>();
497501
}

0 commit comments

Comments
 (0)