use of org.craftercms.commons.validation.ValidationResult in project commons by craftercms.
the class ValidateParamsAspect method doValidation.
@Before("@within(org.craftercms.commons.validation.annotations.param.ValidateParams) || " + "@annotation(org.craftercms.commons.validation.annotations.param.ValidateParams)")
public void doValidation(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
Method method = AopUtils.getActualMethod(joinPoint);
Annotation[][] allParamAnnotations = method.getParameterAnnotations();
ValidationResult result = new ValidationResult(errorMessageBundle);
if (ArrayUtils.isNotEmpty(allParamAnnotations)) {
for (int i = 0; i < args.length; i++) {
Object param = args[i];
Annotation[] paramAnnotations = allParamAnnotations[i];
for (Annotation annotation : paramAnnotations) {
validateParam(annotation, param, result);
}
}
}
if (result.hasErrors()) {
String methodStr = method.toGenericString();
result.setMessage(ValidationUtils.getErrorMessage(errorMessageBundle, INVALID_METHOD_PARAMS_ERROR_CODE, methodStr));
throw new ValidationRuntimeException(result);
}
}
use of org.craftercms.commons.validation.ValidationResult in project commons by craftercms.
the class ValidationAwareRestExceptionHandlers method handleHttpMessageNotReadable.
@Override
protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest webRequest) {
Throwable cause = ExceptionUtils.getRootCause(ex);
if (cause instanceof UnrecognizedPropertyException) {
UnrecognizedPropertyException upe = (UnrecognizedPropertyException) cause;
String field = upe.getPropertyName();
ValidationResult result = new ValidationResult();
result.addError(field, ValidationUtils.getErrorMessage(errorMessageBundle, FIELD_UNRECOGNIZED_ERROR_CODE));
return handleExceptionInternal(ex, result, new HttpHeaders(), HttpStatus.BAD_REQUEST, webRequest);
} else {
String message = ValidationUtils.getErrorMessage(errorMessageBundle, INVALID_REQUEST_BODY_ERROR_CODE);
return handleExceptionInternal(ex, message, new HttpHeaders(), HttpStatus.BAD_REQUEST, webRequest);
}
}
Aggregations