Created
September 20, 2016 11:57
-
-
Save akhileshnirapure/5f64953e27822539f463411a0e8f346a to your computer and use it in GitHub Desktop.
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
// 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