Skip to content

Instantly share code, notes, and snippets.

@akhileshnirapure
Created September 20, 2016 11:57
Show Gist options
  • Save akhileshnirapure/5f64953e27822539f463411a0e8f346a to your computer and use it in GitHub Desktop.
Save akhileshnirapure/5f64953e27822539f463411a0e8f346a to your computer and use it in GitHub Desktop.
// Amikiri/src/Amikiri.Api/Infrastructure/Filters/ValidationExceptionFilter.cs
namespace Amikiri.Api.Infrastructure.Filters
{
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http.Filters;
using global::FluentValidation;
public class ValidationExceptionFilter : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
var validationException = actionExecutedContext.Exception as ValidationException;
if (validationException != null)
{
var errors = validationException.Errors
.Select(x => new
{
field = x.PropertyName,
errorMessage = x.ErrorMessage,
value = x.AttemptedValue,
});
var response = new { ValidationErrors = errors };
actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(HttpStatusCode.BadRequest, response);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment