Search in sources :

Example 1 with ExceptionEnums

use of com.dtstack.taier.common.exception.ExceptionEnums in project Taier by DTStack.

the class ResponseAdvisor method exceptionHandler.

@ResponseBody
@ExceptionHandler(value = Throwable.class)
public R exceptionHandler(Throwable e) {
    ExceptionEnums errorCode = ErrorCode.UNKNOWN_ERROR;
    String errorMsg = null;
    RdosDefineException rdosDefineException = null;
    if (e.getCause() instanceof DtCenterDefException) {
        rdosDefineException = (RdosDefineException) e.getCause();
        if (rdosDefineException.getErrorCode() != null) {
            errorCode = rdosDefineException.getErrorCode();
        }
        errorMsg = rdosDefineException.getErrorMsg();
        if (e.getCause().getCause() != null) {
            LOGGER.error("", e.getCause().getCause());
        }
    } else if (e instanceof RdosDefineException) {
        rdosDefineException = (RdosDefineException) e;
        if (rdosDefineException.getErrorCode() != null) {
            errorCode = rdosDefineException.getErrorCode();
        }
        errorMsg = rdosDefineException.getErrorMsg();
        if (e.getCause() != null) {
            LOGGER.error("", e.getCause());
        }
    } else {
        errorCode = ErrorCode.SERVER_EXCEPTION;
        errorMsg = ErrorCode.SERVER_EXCEPTION.getDescription();
        LOGGER.error("", e);
    }
    if (errorCode.equals(ErrorCode.PERMISSION_LIMIT)) {
        return R.fail(errorCode.getCode(), errorMsg);
    }
    return R.fail(errorCode.getCode(), errorMsg);
}
Also used : ExceptionEnums(com.dtstack.taier.common.exception.ExceptionEnums) RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) DtCenterDefException(com.dtstack.taier.common.exception.DtCenterDefException) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with ExceptionEnums

use of com.dtstack.taier.common.exception.ExceptionEnums in project Taier by DTStack.

the class ResponseAdvisor method handleValidationException.

/**
 * ValidationException
 */
@ResponseBody
@ExceptionHandler(BindException.class)
public R handleValidationException(BindException e) {
    ExceptionEnums errorCode = ErrorCode.UNKNOWN_ERROR;
    BindingResult bindingResult = e.getBindingResult();
    List<FieldError> fieldErrors = bindingResult.getFieldErrors();
    StringBuilder message = new StringBuilder();
    for (FieldError fieldError : fieldErrors) {
        message.append(fieldError.getField()).append(":").append(fieldError.getDefaultMessage()).append(",");
    }
    return R.fail(errorCode.getCode(), message.toString());
}
Also used : BindingResult(org.springframework.validation.BindingResult) ExceptionEnums(com.dtstack.taier.common.exception.ExceptionEnums) FieldError(org.springframework.validation.FieldError) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

ExceptionEnums (com.dtstack.taier.common.exception.ExceptionEnums)2 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 DtCenterDefException (com.dtstack.taier.common.exception.DtCenterDefException)1 RdosDefineException (com.dtstack.taier.common.exception.RdosDefineException)1 BindingResult (org.springframework.validation.BindingResult)1 FieldError (org.springframework.validation.FieldError)1