Style: C# 12 Primary constructor

This commit is contained in:
Kamigen
2024-03-30 20:59:17 -04:00
parent 786c65410d
commit 8f58311283
50 changed files with 267 additions and 449 deletions

View File

@@ -11,8 +11,8 @@ public class CustomExceptionHandler : IExceptionHandler
public CustomExceptionHandler()
{
// Register known exception types and handlers.
_exceptionHandlers = new()
{
_exceptionHandlers = new Dictionary<Type, Func<HttpContext, Exception, Task>>
{
{ typeof(ValidationException), HandleValidationException },
{ typeof(NotFoundException), HandleNotFoundException },
{ typeof(UnauthorizedAccessException), HandleUnauthorizedAccessException },
@@ -24,16 +24,14 @@ public class CustomExceptionHandler : IExceptionHandler
{
var exceptionType = exception.GetType();
if (_exceptionHandlers.ContainsKey(exceptionType))
{
await _exceptionHandlers[exceptionType].Invoke(httpContext, exception);
return true;
}
if (!_exceptionHandlers.TryGetValue(exceptionType, out Func<HttpContext, Exception, Task>? value)) return false;
await value.Invoke(httpContext, exception);
return true;
return false;
}
private async Task HandleValidationException(HttpContext httpContext, Exception ex)
private static async Task HandleValidationException(HttpContext httpContext, Exception ex)
{
var exception = (ValidationException)ex;
@@ -46,7 +44,7 @@ public class CustomExceptionHandler : IExceptionHandler
});
}
private async Task HandleNotFoundException(HttpContext httpContext, Exception ex)
private static async Task HandleNotFoundException(HttpContext httpContext, Exception ex)
{
var exception = (NotFoundException)ex;
@@ -61,7 +59,7 @@ public class CustomExceptionHandler : IExceptionHandler
});
}
private async Task HandleUnauthorizedAccessException(HttpContext httpContext, Exception ex)
private static async Task HandleUnauthorizedAccessException(HttpContext httpContext, Exception ex)
{
httpContext.Response.StatusCode = StatusCodes.Status401Unauthorized;
@@ -73,7 +71,7 @@ public class CustomExceptionHandler : IExceptionHandler
});
}
private async Task HandleForbiddenAccessException(HttpContext httpContext, Exception ex)
private static async Task HandleForbiddenAccessException(HttpContext httpContext, Exception ex)
{
httpContext.Response.StatusCode = StatusCodes.Status403Forbidden;