public ErrorResult GetError(FluentValidation.ValidationException exception) { var errors = new List <ValidationExceptionDetailProperty>(); if (exception.Errors.Any()) { errors.AddRange(exception.Errors.Select(item => new ValidationExceptionDetailProperty { ErrorMessage = item.ErrorMessage, PropertyValue = item.AttemptedValue, PropertyName = item.PropertyName })); } dynamic details = new { Errors = errors, StackTrace = exception.StackTrace }; var error = new Error(DefaultInstance, ErrorCode.System.ToString(), "Request Model Validation Failed", details); return(new ErrorResult(error)); }
private static string SerializeValidationException(FluentValidation.ValidationException ex) { return(JsonConvert.SerializeObject(new { Code = "400", Message = ex.Errors.ToString() })); }
public static void AddModelErrors(this ModelStateDictionary state, FluentValidation.ValidationException exception) { foreach (var error in exception.Errors) { state.AddModelError(error.PropertyName, error.ErrorMessage); } }
private FluentValidationsResult(IEnumerable <ValidationFailure> errors = null) { if (errors != null) { Error = new FluentValidation.ValidationException(errors); } IsValid = Error == null; }
public static FluentValidation.ValidationException CreateError( this FluentValidation.ValidationException exception, string propertyName, string message, string errorCode) { return(new FluentValidation.ValidationException(errorCode, new List <ValidationFailure>() { new ValidationFailure(propertyName, message) { ErrorCode = errorCode } })); }
public void HandleFluentValidationException(FluentValidationException exception, ExceptionContext context) { foreach (var error in exception.Errors) { context.ModelState.AddModelError(error.PropertyName, error.ErrorMessage); } var details = new ValidationProblemDetails(context.ModelState); context.Result = new BadRequestObjectResult(details); context.ExceptionHandled = true; }
public FluentValidationProblemDetails(FluentValidation.ValidationException ex, int statusCode) : base(statusCode) { Errors = ex.Errors?.GroupBy(x => x.PropertyName) .ToDictionary(x => x.Key, x => x.Select(error => new FluentValidationErrors() { AttemptedValue = error.AttemptedValue, CustomState = error.CustomState, ErrorCode = error.ErrorCode, ErrorMessage = error.ErrorMessage, FormattedMessagePlaceholderValues = error.FormattedMessagePlaceholderValues, ResourceName = error.ResourceName, } ).ToArray()); }
public FluentValidationProblemDetails(FluentValidation.ValidationException ex, int statusCode) : base(statusCode) { Errors = ex.Errors?.GroupBy(x => x.PropertyName) .ToDictionary(x => x.Key, x => x.Select(error => new FluentValidationErrors() { AttemptedValue = error.AttemptedValue, CustomState = error.CustomState, ErrorCode = error.ErrorCode, ErrorMessage = error.ErrorMessage, FormattedMessagePlaceholderValues = error.FormattedMessagePlaceholderValues, } ).ToArray()); Detail = string.Join(Environment.NewLine, Errors?.SelectMany(s => s.Value.Select(x => x.ErrorMessage))); }
System.Threading.Tasks.Task HandleExceptionAsync (Microsoft.AspNetCore.Http.HttpContext context, System.Exception exception) { FluentResults.Result result = new FluentResults.Result(); FluentValidation.ValidationException validationException = exception as FluentValidation.ValidationException; if (validationException != null) { var code = System.Net.HttpStatusCode.BadRequest; context.Response.StatusCode = (int)code; context.Response.ContentType = "application/json"; foreach (var error in validationException.Errors) { result.WithError(error.ErrorMessage); } } else { // Log Error! var code = System.Net.HttpStatusCode.InternalServerError; context.Response.StatusCode = (int)code; context.Response.ContentType = "application/json"; result.WithError("Internal Server Error!"); } var options = new System.Text.Json.JsonSerializerOptions { IncludeFields = true, PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase, }; string resultString = System.Text.Json.JsonSerializer.Serialize(value: result, options: options); return(context.Response.WriteAsync(resultString)); }
private async Task HandleValidationExceptionAsync(HttpContext context, ValidationException exception) { var response = context.Response; response.ContentType = "application/json"; response.StatusCode = 400; var errors = exception.Errors.Select(e => new { e.PropertyName, e.ErrorMessage }).ToList(); var errorText = JsonConvert.SerializeObject(errors); await response.WriteAsync(errorText); }
public static OaoValidationException ToQ2Exception(this ValidationException ex) { var q2Exception = new OaoValidationException(ex.Message); if (ex.Errors.SafeAny()) { q2Exception.Errors = ex.Errors.Select( x => new OaoValidationError { AttemptedValue = x.AttemptedValue, ErrorCode = x.ErrorCode, Message = x.ErrorMessage, Property = x.PropertyName }); } return(q2Exception); }
public static GeneralError ToGeneralError(this FluentValidation.ValidationException exception) { var generalError = new GeneralError(); foreach (var failure in exception.Errors) { if (generalError.Errors == null) { generalError.Errors = new List <Error>(); } generalError.Errors.Add(new Error { ErrorCode = failure.ErrorCode, UserMessage = failure.ErrorMessage, PropertyName = failure.PropertyName, Type = exception.GetType().ToString(), DeveloperMessage = Properties.Settings.Default.IncludeExceptionDetail ? exception.StackTrace : null }); } return(generalError); }
public static ExceptionModel ToHttpException(this Exception e) { return(e switch { FluentValidation.ValidationException exception => new ValidationExceptionModel { Message = e.Message, InnerException = e?.InnerException?.ToHttpException(), StackTrace = e.StackTrace, ValidationErrors = exception.Errors.Select(x => new ValidationErrorModel { Message = x.ErrorMessage, PropertyName = x.PropertyName, AttemptedValue = x.AttemptedValue, ErrorCode = x.ErrorCode }) .ToList() }, System.ComponentModel.DataAnnotations.ValidationException exception => new ValidationExceptionModel() { Message = e.Message, InnerException = e?.InnerException?.ToHttpException(), StackTrace = e.StackTrace, ValidationErrors = exception?.ValidationResult?.MemberNames?.Select(x => new ValidationErrorModel { Message = (e as System.ComponentModel.DataAnnotations.ValidationException) ?.ValidationResult?.ErrorMessage ?? String.Empty, PropertyName = x }) ?.ToList() }, _ => new ExceptionModel { Message = e.Message, InnerException = e?.InnerException?.ToHttpException(), StackTrace = e.StackTrace } });
public static APIGatewayProxyResponse CreateResponse(this FluentValidation.ValidationException exception) { return(CreateApiGatewayResponse(HttpStatusCode.BadRequest, exception.InnerException, exception.Message)); }
public OrderingDomainException(string message, FluentValidation.ValidationException validationException, System.Collections.Generic.List <FluentValidation.Results.ValidationFailure> failtures) : base(message) { }
public RequestValidationExceptionProblemDetails(FluentValidation.ValidationException exception) { this.Title = exception.Message; this.Status = StatusCodes.Status400BadRequest; }