Skip to content

Instantly share code, notes, and snippets.

@luanvuhlu
Last active June 19, 2017 08:00
Show Gist options
  • Save luanvuhlu/109ca719c568ec7ef2d59b18d1b95022 to your computer and use it in GitHub Desktop.
Save luanvuhlu/109ca719c568ec7ef2d59b18d1b95022 to your computer and use it in GitHub Desktop.
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public ValidationError handleValidationException(MethodArgumentNotValidExceptionexception) {
Set<ValidationError> errors = new HashSet<ValidationError>();
for (ObjectError er : exception.getBindingResult().getAllErrors()) {
errors.add(new ValidationError(er.getObjectName(), er.getDefaultMessage()));
}
return new ValidationErrorResponse(errs);
}
// Function that Makes Ajax call
function doesSomeStuff() {
$.ajax({
type: "POST",
url: "some/url/in/your/app",
// ... etc ...
error: function(data) {
if (data.errorMessages != null) {
handleAjaxError(data);
return;
}
// ... etc ...
}
});
}
// function that handles ajax error
function handleAjaxError(data) {
var $errorMessageArea = $("#errorMessageArea");
for(var i = 0; i < data.errorMessages.length; i++) {
// append the current error message to the element on the web page
$errorMessageArea.append($("<li />", { text: data.errorMessages[i] }));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment