记录一下自己对项目中异常的处理封装,通用组件

时间:2024-01-27 15:22:57

总的exception包含三个类

 

 

 1. BaseAppException

public class BaseAppException extends BaseException {

    public BaseAppException() {
        super();
    }

    public BaseAppException(String errorMessage) {
        super(errorMessage);
    }

    public BaseAppException(ResponseStatus responseStatus) {
        super(responseStatus);
    }

    public BaseAppException(ResponseStatus responseStatus, Throwable cause) {
        super(responseStatus, cause);
    }

    public BaseAppException(String errorMessage, BaseAppException e) {
        super(errorMessage, e);
    }

    public BaseAppException(Throwable cause) {
        super(cause);
    }


    public BaseAppException(String errorCode, String errorMessage) {
        super(errorCode,errorMessage);
    }

2. BaseErrException

public class BaseErrException extends BaseException {

    private static final long serialVersionUID = 1L;

    public BaseErrException(String errorMessage) {
        super(errorMessage);
    }

    public BaseErrException(String errorMessage, BaseErrException e) {
        super(errorMessage, e);
    }

    public BaseErrException(Throwable cause) {
        super(cause);
    }
}

3.BaseException

public abstract class BaseException extends RuntimeException {

    private static final long serialVersionUID = 1L;

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

    /**
     * 错误描述
     */
    private String errorMessage;

    /**
     * 异常触发原因
     */
    private Throwable cause;

    public BaseException() {
    }

    public BaseException(String errorMessage) {
        this.cause = new Throwable(errorMessage);
        this.errorMessage = errorMessage;
        this.errorCode = ResponseStatus.FAIL_COMMOM.code;
    }

    public BaseException(ResponseStatus responseStatus) {
        this.errorCode = responseStatus.code;
        this.errorMessage = responseStatus.msg;
        this.cause = new Throwable(responseStatus.msg + ". [errCode:" + responseStatus.code + "]");
    }

    public BaseException(ResponseStatus responseStatus, Throwable cause) {
        this.errorCode = responseStatus.code;
        this.errorMessage = responseStatus.msg;
        this.cause = cause;
    }

    public BaseException(String errorMessage, BaseException e) {
        String causeErrMsg = "";
        if(null != e) {
            if (null != e.getCause()) {
                causeErrMsg = " Caused by :" + e.getCause().getMessage();
            }
            this.errorCode = e.getErrorCode();
            this.errorMessage = errorMessage + causeErrMsg;
            this.cause = e.getCause();
        } else {
            this.errorMessage = errorMessage;
            this.errorCode = ResponseStatus.FAIL_COMMOM.code;
            this.cause = new Throwable(errorMessage + "[errCode:" + ResponseStatus.FAIL_COMMOM.code + "]");
        }
    }

    public BaseException(Throwable cause) {
        super(cause);
    }


    public BaseException(String errorCode, String errorMessage) {
        // super(showMessage);
        this.errorMessage = errorMessage;
        this.errorCode = errorCode;
    }

    public String getErrorCode() {
        return errorCode;
    }

    @Override
    public Throwable getCause() {
        return cause;
    }

    public void setCause(Throwable cause) {
        this.cause = cause;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    /**
     * Description:获取异常信息
     *
     * @return
     */
    @Override
    public String getMessage() {
        String msg = super.getMessage();
        String causeMsg = null;
        if (this.cause != null) {
            causeMsg = this.cause.getMessage();
        }
        if (msg != null) {
            if (causeMsg != null) {
                return msg + " caused by: " + causeMsg;
            }
            return msg;
        }
        return causeMsg;
    }

}

3. ResponseStatus 枚举

public enum ResponseStatus {

    // 成功
    SUCCESS("S0001","成功"),
    // 成功,结果为空
    SUCCESS_RESULT_IS_NULL("S0002","结果为空"),
 /**
     * 编码
     */
    public String code;
    /**
     * 信息
     */
    public String msg;

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

 

2. 上面的异常,在service、dao、一直向上层抛出,到web中,返回接住

 @Override
    public CategoryRes insertory(CateyReq req) {
       XXXXX
        logger.info("新增类目,param={}", req);

        CategoryRes res = new CategoryRes();
        try {
            CategoryVo vo = this.handleInsertParam(req);
            // 保存数据
            categoryService.insertCategory(vo);
            // 返回保存后数据 的id
            res.setCategoryExVo(new CategoryExVo(vo.getId()));
        } catch (Exception e) {
            res = new CategoryRes(ExceptionUtils.handleException(new Exception(e), info));
        } finally {
            Profiler.registerInfoEnd(info);
        }

        return res;
    }
ExceptionUtils.handleException 的信息
public CategoryRes(Result result) {
this.result = result;
}

统一的返回结果Result

public class Result implements Serializable{

    /**
     * 结果标志
     */
    private boolean isSuccess;
    /**
     * 结果码
     */
    private String code;
    /**
     * 结果信息描述
     */
    private String msg;

    public Result(){
    }

    /**
     * 未发生异常
     * @param rpcStatus
     */
    public Result(RpcStatus rpcStatus) {
        this.code = rpcStatus.getCode();
        this.msg = rpcStatus.getMsg();
        this.setSuccess(RpcStatus.FAIL != rpcStatus);
    }

    /**
     * 发生程序补获并处理的异常(RpcException)时,errCode, errMessage
     * @param e
     */
    public Result(RpcException e) {
        this.code = e.getErrorCode();
        this.msg = e.getErrorMessage();
        this.setSuccess(false);
    }

    /**
     * 自定义
     * @param code
     * @param msg
     */
    public Result(String code, String msg) {
        this.code = code;
        this.msg = msg;
        this.setSuccess(false);
    }

    public boolean isSuccess() {
        return isSuccess;
    }

    public void setSuccess(boolean success) {
        isSuccess = success;
    }


    public String getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

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

}
public class ExceptionUtils {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionUtils.class);


    /**
     * 分析处理RpcException
     * @param e
     * @param info
     * @return
     */
    public static final Result handleException(Exception e, CallerInfo info) {
        Throwable cause = null;
        if (e.getCause() != null) {
            cause = e.getCause();
        } else {
            cause = e;
        }

        if (cause.getClass().equals(BaseAppException.class)) {
            // BaseAppException 逻辑判断不通过
            BaseAppException e1 = (BaseAppException) e.getCause();
            return new Result(new RpcException(e1.getErrorCode(), e1.getErrorMessage()));
        } else if (cause.getClass().equals(BaseErrException.class)) {
            // BaseErrException 捕获到的异常
            BaseErrException e1 = (BaseErrException) e.getCause();
            return new Result(e1.getErrorCode(), e1.getErrorMessage());
        } else {
            // 未知异常
            if (null != info) {
                Profiler.functionError(info);
            }
            return new Result(new RpcException(RpcStatus.FAIL.code, RpcStatus.FAIL.msg));
        }
    }
}