Skip to content

Instantly share code, notes, and snippets.

@JayGhb
Last active August 10, 2023 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JayGhb/86c3074f9ab4c8c12c6780f3d9e8bc4a to your computer and use it in GitHub Desktop.
Save JayGhb/86c3074f9ab4c8c12c6780f3d9e8bc4a to your computer and use it in GitHub Desktop.
Validation Exceptions handling to combine with existing exception handling
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