com.erigir.wrench.ape.exception.DataValidationException Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wrench-ape Show documentation
Show all versions of wrench-ape Show documentation
A library to simplify API construction atop Spring MVC
package com.erigir.wrench.ape.exception;
import com.erigir.wrench.ape.http.ApeException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import java.util.Map;
import java.util.TreeMap;
/**
* Created by chrweiss on 6/28/14.
*/
@ApeException(
httpStatusCode = 400,
detailCode = 103,
message = "There are invalid values in the data submitted",
developerMessage = "Data does not pass schema validation - check json schema and details object",
detailObjectPropertyName = "errorMap"
)
public class DataValidationException extends RuntimeException {
private Map errorMap;
public DataValidationException(BindingResult errors) {
super();
errorMap = toErrorMap(errors);
}
public DataValidationException(Map errorMap) {
super();
this.errorMap = errorMap;
}
private static Map toErrorMap(BindingResult errors) {
TreeMap rval = new TreeMap<>();
for (FieldError fe : errors.getFieldErrors()) {
rval.put(fe.getField(), fe.getDefaultMessage());
}
return rval;
}
public Map getErrorMap() {
return errorMap;
}
}