Last active
August 10, 2023 11:53
-
-
Save JayGhb/86c3074f9ab4c8c12c6780f3d9e8bc4a to your computer and use it in GitHub Desktop.
Validation Exceptions handling to combine with existing exception handling
This file contains hidden or 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
if (exception is ValidationException validationException) | |
{ | |
string[] validationErrorMessages = validationException.Errors.Select(x => x.ErrorMessage).ToArray(); | |
ValidationProblemDetails problemDetails = new ValidationProblemDetails | |
{ | |
Instance = req.Path, | |
Status = StatusCodes.Status400BadRequest, | |
Title = "One or more validation errors occurred.", | |
Detail = $"{string.Join('-', validationErrorMessages)}" | |
}; | |
problemDetails.Errors.Add("CommandValidationErrors", validationErrorMessages); | |
return new BadRequestObjectResult(problemDetails); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment