全局异常处理

通用异常拦截器 

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public RespResult exceptionHandler(HttpServletRequest req, Exception e) throws Exception {

        RespResult respResult;

        if (e instanceof RRException) {
            RRException bizException = (RRException) e;
            respResult = RespUtil.error(bizException.getCode(), bizException.getMessage());
        } else {
            respResult = RespUtil.error(ErrorCodeEnum.FAILED.getCode(), e.getMessage());
        }

        return respResult;
    }
}

异常返回类

public class RespResult {
    private int code;
    private String msg;
    private Object data;

    public RespResult(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
public class RespUtil {
    /*
    public static RespResult success(Object data) {

        RespResult respResult = new RespResult(ErrorCodeEnum.SUCCESS.getCode(), ErrorCodeEnum.SUCCESS.getMsg(), data);
        return respResult;
    }
    */

    public static RespResult error(Integer code, String msg) {
        RespResult respResult = new RespResult(code, msg, null);
        return respResult;
    }
}

 

自定义异常 

/**
 * 自定义异常
 *
 * @author fanghui
 */
public class RRException extends RuntimeException {
    private static final long serialVersionUID = 1L;

    private int code;

    public RRException(String msg) {
        super(msg);
    }

    public RRException(String msg, Throwable e) {
        super(msg, e);
    }

    public RRException(String msg, int code) {
        super(msg);
        this.code = code;
    }

    public RRException(String msg, int code, Throwable e) {
        super(msg, e);
        this.code = code;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

}

 异常常量

public enum ErrorCodeEnum implements IErrorCode {
    /**
     * success
     */
    SUCCESS(0, "success"),
    /**
     * failed
     */
    FAILED(10000, "failed"),
    /**
     * token is empty
     */
    TOKEN_IS_EMPTY(10001, "token is empty"),
    /**
     * token is invalid
     */
    TOKEN_IS_INVALID(10002, "token is invalid"),
    /**
     * token is expired
     */
    TOKEN_IS_EXPIRED(10003, "token is expired"),
    /**
     * 列字段超长
     */
    DB_DATA_TOO_LONG(10004, "column is to long"),
    /**
     * KMS环境执行失败
     */
    KMS_EXECUTE_ERROR(10005, "KMS environment execution failed"),
    /**
     * 用户登录信息缺失,请重新登录
     */
    USER_LOGIN_ERROR(10006, "user login information is missing, please login again"),
    /**
     * 请求参数为空
     */
    REQUEST_PARAM_ERROR(10007, "request parameter exception"),
    /**
     * 查询结果为空
     */
    QUERY_RESULT_NULL(10008, "query result is empty"),
    /**
     * 状态异常
     */
    STATUS_ERROR(10009, "status error"),
    /**
     * 参数转化异常
     */
    PARAM_CONVERT_ERROR(10010, "parameter conversion error"),
    /**
     * 参数不合法
     */
    PARAM_INVALID_ERROR(10011, "parameter invalid error"),
    /**
     * excel导出异常
     */
    EXPORT_EXCEL_FAILED(10012, "export excel error"),
    /**
     * oss url获取异常
     */
    OSS_OPERATION_FAILED(10013, "oss operation error"),
    /**
     * es 操作异常
     */
    ES_OPERATION_FAILED(10014, "es operation error"),
    /**
     * 日期 操作异常
     */
    DATE_OPERATION_FAILED(10015, "date operation error"),
    /**
     * 没有权限 操作异常
     */
    NO_PERMISSION_FAILED(10016, "no permission error");

    private final int code;

    private final String msg;

    ErrorCodeEnum(int code, String msg) {
        this.msg = msg;
        this.code = code;
    }

    @Override
    public int getCode() {
        return this.code;
    }

    @Override
    public String getMsg() {
        return this.msg;
    }

    public static String getMsgByCode(int code) {
        ErrorCodeEnum[] values = ErrorCodeEnum.values();
        for (ErrorCodeEnum value : values) {
            if (value.code == code) {
                return value.msg;
            }
        }
        return StringUtils.EMPTY;
    }
}

 

public interface IErrorCode {

    /**
     * @return code
     * @see ErrorCodeEnum
     */
    int getCode();

    /**
     * @return msg
     * @see ErrorCodeEnum
     */
    String getMsg();

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值