-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed exception handler to use the new IExceptionHandler feature in…
… NET8
- Loading branch information
1 parent
05a02b2
commit cf0d730
Showing
5 changed files
with
59 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 0 additions & 83 deletions
83
src/API/CleanArc.WebFramework/Middlewares/CustomExceptionHandlerMiddleware.cs
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
src/API/CleanArc.WebFramework/Middlewares/ExceptionHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using CleanArc.Application.Models.ApiResult; | ||
using FluentValidation; | ||
using Microsoft.AspNetCore.Diagnostics; | ||
using Microsoft.AspNetCore.Http; | ||
using System.ComponentModel.DataAnnotations; | ||
using CleanArc.SharedKernel.Extensions; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace CleanArc.WebFramework.Middlewares; | ||
|
||
public class ExceptionHandler(ILogger<ExceptionHandler> logger,IWebHostEnvironment environment) : IExceptionHandler | ||
{ | ||
|
||
public async ValueTask<bool> TryHandleAsync(HttpContext context, Exception exception, CancellationToken cancellationToken) | ||
{ | ||
if (exception is FluentValidation.ValidationException validationException) | ||
{ | ||
context.Response.StatusCode = StatusCodes.Status422UnprocessableEntity; | ||
|
||
var errors = new Dictionary<string, List<string>>(); | ||
|
||
foreach (var validationExceptionError in validationException.Errors) | ||
{ | ||
if (!errors.ContainsKey(validationExceptionError.PropertyName)) | ||
errors.Add(validationExceptionError.PropertyName, new List<string>() { validationExceptionError.ErrorMessage }); | ||
else | ||
errors[validationExceptionError.PropertyName].Add(validationExceptionError.ErrorMessage); | ||
|
||
} | ||
|
||
var apiResult = new ApiResult<IDictionary<string, List<string>>>(false, ApiResultStatusCode.EntityProcessError, errors, ApiResultStatusCode.EntityProcessError.ToDisplay()); | ||
|
||
context.Response.ContentType = "application/problem+json"; | ||
await context.Response.WriteAsJsonAsync(apiResult, cancellationToken: cancellationToken); | ||
|
||
return true; | ||
} | ||
|
||
logger.LogError(exception,"Error captured in global exception handler"); | ||
|
||
context.Response.StatusCode = StatusCodes.Status500InternalServerError; | ||
|
||
if (environment.IsDevelopment()) | ||
return false; | ||
|
||
context.Response.ContentType = "application/problem+json"; | ||
var response = new ApiResult(false, | ||
ApiResultStatusCode.ServerError, "Server Error"); | ||
context.Response.StatusCode = StatusCodes.Status500InternalServerError; | ||
await context.Response.WriteAsJsonAsync(response, cancellationToken: cancellationToken); | ||
|
||
return true; | ||
} | ||
} |