业务异常通用类及全局异常处理

一,异常抛出方式

方式1

if (eneity == null) {
    throw new WmsException(CommonCode.UPDATE_MONGO_ERROR);
}

方式2

if (subjectEntity == null || subjectEntity.getDictEntities() == null || CollectionUtils.isEmpty(subjectEntity.getDictEntities())){
   logger.error("参数异常");
   throw new WmsException(RespCode.PARAM_IS_NULL, RespMsg.PARAM_IS_NULL_MSG);
}

二,业务异常处理类

方式1

//错误代码
ResultCodeArc resultCode;

异常构造方法
public WmsException(ResultCodeArc resultCode){
    this.resultCode = resultCode;
}

错误码及信息接口
public interface ResultCodeArc {
    //操作代码
    String code();
    //提示信息
    String message();
}

错误码及信息,定义类
@ToString
public enum CommonCode implements ResultCodeArc{
    INVALID_PARAM("10003","非法参数!"),
    SUCCESS("10000","操作成功!"),
    FAIL("11111","操作失败!");
    //操作代码
    String code;
    //提示信息
    String message;
    private CommonCode(String code, String message){
        this.code = code;
        this.message = message;
    }

    @Override
    public String code() {
        return code;
    }

    @Override
    public String message() {
        return message;
    }

}

方式2

/**
 * 构造一个基本异常.
 * @param errorCode     错误编码
 * @param message       信息描述
 */
public WmsException(String errorCode, String message) {
    this(errorCode, message, true);
}

错误码及信息,定义类

错误码类

public final class RespCode {

 /** 0000 成功 */
  public static final String SUCCESS = "0000";
}

错误信息类

public final class RespMsg {
    /**
     * 0000 成功
     */
    public static final String SUCCESS_MSG = "成功";
}

三,业务异常处理类

/**
 * 异常类
 */
public class WmsException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    /**
     * 错误编码
     */
    private String errorCode;

    /**
     * 错误信息
     */
    private String errorMsg;

    /**
     * 消息是否为属性文件中的Key
     */
    private boolean propertiesKey = true;

    //错误代码
    ResultCodeArc resultCode;

    public WmsException(ResultCodeArc resultCode){
        this.resultCode = resultCode;
    }
    public ResultCodeArc getResultCode(){
        return resultCode;
    }

    /**
     * 构造一个基本异常.
     * @param message       信息描述
     */
    public WmsException(String message) {
        super(message);
    }
    /**
     * 构造一个基本异常.
     * @param errorCode     错误编码
     * @param message       信息描述
     */
    public WmsException(String errorCode, String message) {
        this(errorCode, message, true);
    }

    /**
     * 构造一个基本异常.
     * @param errorCode     错误编码
     * @param message       信息描述
     * @param cause         根异常类(可以存入任何异常)
     */
    public WmsException(String errorCode, String message, Throwable cause) {
        this(errorCode, message, cause, true);
    }

    /**
     * 构造一个基本异常.
     *
     * @param errorCode         错误编码
     * @param message           信息描述
     * @param propertiesKey     消息是否为属性文件中的Key
     */
    public WmsException(String errorCode, String message, boolean propertiesKey) {
        super(message);
        this.setErrorMsg(message);
        this.setErrorCode(errorCode);
        this.setPropertiesKey(propertiesKey);
    }

    /**
     * 构造一个基本异常.
     *
     * @param errorCode         错误编码
     * @param message           信息描述
     * @param cause             根异常类(可以存入任何异常)
     * @param propertiesKey     消息是否为属性文件中的Key
     */
    public WmsException(String errorCode, String message, Throwable cause, boolean propertiesKey) {
        super(message, cause);
        this.setErrorCode(errorCode);
        this.setPropertiesKey(propertiesKey);
    }

    /**
     * 构造一个基本异常.
     *
     * @param message       信息描述
     * @param cause         根异常类(可以存入任何异常)
     */
    public WmsException(String message, Throwable cause) {
        super(message, cause);
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public boolean isPropertiesKey() {
        return propertiesKey;
    }

    public void setPropertiesKey(boolean propertiesKey) {
        this.propertiesKey = propertiesKey;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }
}

四,统一异常处理

/**
 * <pre>
 * GlobalExceptionHandler
 * </pre>
 *
 * @author wanglong
 * @version $Id: GlobalExceptionHandler.java, v 0.1 2018年3月16日 上午9:40:56 wanglong Exp $
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger LOGGER = LogManager.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public BaseResponseModel<Object> defaultErrorHandler(Exception e, HttpServletRequest request) {
        BaseResponseModel<Object> response = new BaseResponseModel<>();
        LOGGER.info("请求地址:" + request.getRequestURL());
        LOGGER.error("异常信息:", e);
        response.setRepCode(RespCode.ACT_EXCEPTION);
        response.setRepMsg(RespMsg.ACT_EXCEPTION_MSG);
        return response;
    }

    /**
     * @param wmsException
     * @return
     */
    //捕获WmsException此类异常
    @ExceptionHandler(WmsException.class)
    @ResponseBody
    public BaseResponseModel<Object> wmsException(WmsException wmsException) {
        BaseResponseModel<Object> response = new BaseResponseModel<>();
        LOGGER.error("catch exception:{}", wmsException.getMessage());
        /**
         * update by xuyongkun 2019-9-17 扩展了自定义异常信息的捕捉,不限于CommonCode枚举类中的异常信息
        response.setRepCode(wmsException.resultCode.code());
        response.setRepMsg(wmsException.resultCode.message());
        */
        if (wmsException.resultCode != null) {
            response.setRepCode(wmsException.resultCode.code());
            response.setRepMsg(wmsException.resultCode.message());
        } else {
            response.setRepCode(wmsException.getErrorCode());
            response.setRepMsg(wmsException.getErrorMsg());
        }
        return response;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值