Search in sources :

Example 1 with ErrorInfo

use of com.jim.framework.web.common.ErrorInfo in project jim-framework by jiangmin168168.

the class BaseController method exception.

@ExceptionHandler
public ValueResult exception(HttpServletRequest request, Exception ex) {
    ValueResult result = new ValueResult();
    if (ex instanceof ConstraintViolationException) {
        ConstraintViolationException constraintViolationException = (ConstraintViolationException) ex;
        Set<ConstraintViolation<?>> constraintViolations = constraintViolationException.getConstraintViolations();
        String errorMsg = this.getConstraintViolationsMsg(constraintViolations);
        result.setError(new ErrorInfo(ErrorDef.InvalidParameters.getCode(), errorMsg));
        result.setResult(false);
    } else if (ex instanceof MethodArgumentNotValidException) {
        MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) ex;
        List<ObjectError> allErrors = methodArgumentNotValidException.getBindingResult().getAllErrors();
        String errorMsg = this.getAllParametersInValidMsg(allErrors);
        result.setError(new ErrorInfo(ErrorDef.InvalidParameters.getCode(), errorMsg));
    } else {
        logger.error("system error", ex);
        result.setError(new ErrorInfo(ErrorDef.ServerError.getCode(), ex.getMessage()));
        result.setResult(false);
    }
    return result;
}
Also used : ValueResult(com.jim.framework.web.common.ValueResult) ConstraintViolation(javax.validation.ConstraintViolation) ErrorInfo(com.jim.framework.web.common.ErrorInfo) ConstraintViolationException(javax.validation.ConstraintViolationException) List(java.util.List) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Aggregations

ErrorInfo (com.jim.framework.web.common.ErrorInfo)1 ValueResult (com.jim.framework.web.common.ValueResult)1 List (java.util.List)1 ConstraintViolation (javax.validation.ConstraintViolation)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)1 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)1