Search in sources :

Example 1 with ConstraintViolationInfo

use of com.haulmont.restapi.exception.ConstraintViolationInfo in project cuba by cuba-platform.

the class RestControllerExceptionHandler method handleCustomValidationException.

@ExceptionHandler(CustomValidationException.class)
@ResponseBody
public ResponseEntity<List<ConstraintViolationInfo>> handleCustomValidationException(CustomValidationException e) {
    log.debug("CustomValidationException: {}", e.getMessage());
    ConstraintViolationInfo errorInfo = new ConstraintViolationInfo();
    errorInfo.setPath("");
    errorInfo.setInvalidValue(null);
    errorInfo.setMessage(e.getLocalizedMessage());
    errorInfo.setMessageTemplate("{com.haulmont.cuba.core.global.validation.CustomValidationException}");
    return new ResponseEntity<>(Collections.singletonList(errorInfo), HttpStatus.BAD_REQUEST);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ConstraintViolationInfo(com.haulmont.restapi.exception.ConstraintViolationInfo) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with ConstraintViolationInfo

use of com.haulmont.restapi.exception.ConstraintViolationInfo in project cuba by cuba-platform.

the class RestControllerExceptionHandler method getConstraintViolationInfos.

protected List<ConstraintViolationInfo> getConstraintViolationInfos(Set<ConstraintViolation<?>> violations) {
    List<ConstraintViolationInfo> violationInfos = new ArrayList<>();
    for (ConstraintViolation<?> violation : violations) {
        ConstraintViolationInfo info = new ConstraintViolationInfo();
        info.setMessage(violation.getMessage());
        info.setMessageTemplate(violation.getMessageTemplate());
        Object invalidValue = violation.getInvalidValue();
        if (invalidValue != null) {
            Class<?> invalidValueClass = invalidValue.getClass();
            boolean serializable = false;
            for (Class serializableType : serializableInvalidValueTypes) {
                // noinspection unchecked
                if (serializableType.isAssignableFrom(invalidValueClass)) {
                    serializable = true;
                    break;
                }
            }
            if (serializable) {
                info.setInvalidValue(invalidValue);
            } else {
                info.setInvalidValue(null);
            }
        }
        if (violation.getPropertyPath() != null) {
            info.setPath(violation.getPropertyPath().toString());
        }
        violationInfos.add(info);
    }
    return violationInfos;
}
Also used : ConstraintViolationInfo(com.haulmont.restapi.exception.ConstraintViolationInfo)

Aggregations

ConstraintViolationInfo (com.haulmont.restapi.exception.ConstraintViolationInfo)2 ResponseEntity (org.springframework.http.ResponseEntity)1 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1