-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: configuration fluent validation
- Loading branch information
1 parent
2b9ccad
commit d4f809b
Showing
7 changed files
with
65 additions
and
82 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 was deleted.
Oops, something went wrong.
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
23 changes: 23 additions & 0 deletions
23
Living.WebAPI/ExceptionsHandler/FluentValidationExceptionHandler.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,23 @@ | ||
using FluentValidation; | ||
using Living.Domain.Base; | ||
using Microsoft.AspNetCore.Diagnostics; | ||
|
||
namespace Living.WebAPI.ExceptionsHandler; | ||
|
||
public class FluentValidationExceptionHandler : IExceptionHandler | ||
{ | ||
public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken) | ||
{ | ||
if (exception is ValidationException validation) | ||
{ | ||
var notifications = validation.Errors.Select(e => new Notification(e.PropertyName.ToUpper(), e.ErrorCode)); | ||
|
||
httpContext.Response.StatusCode = StatusCodes.Status400BadRequest; | ||
await httpContext.Response.WriteAsJsonAsync(new BaseResponse(notifications.DistinctBy(p => p.Code)), cancellationToken); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
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