自定義業務異常處理


要處理什么異常:

1,基礎異常(參數異常),輸入為空,格式不正確。

2,業務異常,未實現效果,拋出異常給予提示。如:圖片上傳影像庫,未正確返回影像id;

   // 上傳至影像庫,得到影像ID
        String imageId = pdfPy.pdf(pySzCisReportRoot, pyQueryBean.getUmName());
        if (StringUtils.isEmpty(imageId)) {
            throw new CreditException(PyCreditServiceErrorEnum.FAILED_UPLOAD_UDMP.getCode(), PyCreditServiceErrorEnum.FAILED_UPLOAD_UDMP.getMsg());
        }

3,服務異常,調用別人接口,服務超時,服務不可用(未能正常返回接口返回Document)
4,不知道的什么異常。除了以上異常外,給別人提供接口,不知道哪里就異常了。

怎么處理:

當然是Service業務邏輯處理時往外拋,在Controller中捕獲。

Contrller代碼:

把以上提到的四種異常分2類處理

先捕獲123業務異常,再捕獲未知異常。

 /**
     * 反欺詐
     *
     * @return com.pingan.credit.model.ResponseResult<com.pingan.credit.model.py.CisReportRoot>
     * @Description: 鵬元征信 反欺詐接口
     * @author chiyuanzhen743
     * @date 2017/8/25 17:30
     */
    @RequestMapping(path = "/queryPy", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ResponseResult<CisReportRoot> queryPy(HttpServletRequest request, PyQueryBean pyQueryBean) {
        ResponseResult<CisReportRoot> responseResult = new ResponseResult<>();
        try {
            CisReportRoot cisReportRoot = pyQueryService.queryCisReport(pyQueryBean);
            responseResult.setData(cisReportRoot);
            responseResult.setRefID(pyQueryBean.getRefID());
            responseResult.setTimestamp(DateUtil.getTimeStamp().toString());
            responseResult.setRet(SUCCESS);
        } catch (CreditException e) {
            responseResult.setErrorCode(e.getErrorCode());
            responseResult.setRet(SUCCESS);
            responseResult.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            responseResult.setErrorCode(PyCreditServiceErrorEnum.SYSTEM_ERROR.getCode());
            responseResult.setRet(FAILED);
            responseResult.setErrorMsg(PyCreditServiceErrorEnum.SYSTEM_ERROR.getMsg());
        }
        return responseResult;
    }

 

一,應用場景

1,請求參數驗證

代碼:

/**
     * @Description: 查詢參數校驗
     * @author chiyuanzhen743
     * @date 2017/8/24 14:15
     */
    private void isQueryBeanParametersIllegal(PyQueryBean pyQueryBean) throws Exception {
        if (StringUtils.isEmpty(pyQueryBean.getName()) || StringUtils.isEmpty(pyQueryBean.getDocumentNo()) || StringUtils.isEmpty(pyQueryBean.getPhone())) {
            throw new CreditException(PyCreditServiceErrorEnum.CHECK_EMPTY_PARAMETERS.getCode(), PyCreditServiceErrorEnum.CHECK_EMPTY_PARAMETERS.getMsg());
        }
        if (!ValidatorUtil.isPhoneLegal(pyQueryBean.getPhone())) {
            throw new CreditException(PyCreditServiceErrorEnum.CHECK_PHONE.getCode(), PyCreditServiceErrorEnum.CHECK_PHONE.getMsg());
        }
        if (!ValidatorUtil.validateCard(pyQueryBean.getDocumentNo())) {
            throw new CreditException(PyCreditServiceErrorEnum.CHECK_DOCUMENTNO.getCode(), PyCreditServiceErrorEnum.CHECK_DOCUMENTNO.getMsg());
        }
        if (!ValidatorUtil.isChineseNameLegal(pyQueryBean.getName())) {
            throw new CreditException(PyCreditServiceErrorEnum.CHECK_NAME.getCode(), PyCreditServiceErrorEnum.CHECK_NAME.getMsg());
        }
        if (StringUtils.isEmpty(pyQueryBean.getTimestamp())) {
            throw new CreditException(PyCreditServiceErrorEnum.CHECK_TIMESTAMP.getCode(), PyCreditServiceErrorEnum.CHECK_TIMESTAMP.getMsg());
        }
        if (StringUtils.isEmpty(pyQueryBean.getUmName())) {
            throw new CreditException(PyCreditServiceErrorEnum.CHECK_UMNAME.getCode(), PyCreditServiceErrorEnum.CHECK_UMNAME.getMsg());
        }

        if (pyQueryBean.getQueryType() == null || pyQueryBean.getQueryType() == 0) {
            throw new CreditException(PyCreditServiceErrorEnum.EMPTY_QUERYTYPE.getCode(), PyCreditServiceErrorEnum.EMPTY_QUERYTYPE.getMsg());
        }

        if (StringUtils.isEmpty(pyQueryBean.getQueryReasonCode())) {
            throw new CreditException(PyCreditServiceErrorEnum.EMPTY_QUERYREASONCODE.getCode(), PyCreditServiceErrorEnum.EMPTY_QUERYREASONCODE.getMsg());
        }

        if (StringUtils.isEmpty(pyQueryBean.getQueryWay())) {
            throw new CreditException(PyCreditServiceErrorEnum.EMPTY_QUERY_WAY.getCode(), PyCreditServiceErrorEnum.EMPTY_QUERY_WAY.getMsg());
        }

        if (pyQueryBean.getQueryReason() == null || StringUtils.isEmpty(pyQueryBean.getQueryReason())) {
            throw new CreditException(PyCreditServiceErrorEnum.EMPTY_QUERY_REASON.getCode(), PyCreditServiceErrorEnum.EMPTY_QUERY_REASON.getMsg() + " " + pyQueryBean.getQueryReasonCode());
        }

    }

 

2,具體業務處理失敗

如:生成PDF失敗,圖片上傳至影像平台失敗。

二,定義異常處理類

代碼:

public class CreditException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    /**
     * 錯誤編碼
     */
    private String errorCode;

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

    /**
     * 構造一個基本異常.
     *
     * @param message 信息描述
     */
    public CreditException(String message) {
        super(message);
    }

    /**
     * 構造一個基本異常.
     *
     * @param errorCode 錯誤編碼
     * @param message   信息描述
     */
    public CreditException(String errorCode, String message) {
        this(errorCode, message, true);
    }

    /**
     * 構造一個基本異常.
     *
     * @param errorCode 錯誤編碼
     * @param message   信息描述
     */
    public CreditException(String errorCode, String message, Throwable cause) {
        this(errorCode, message, cause, true);
    }

    /**
     * 構造一個基本異常.
     *
     * @param errorCode     錯誤編碼
     * @param message       信息描述
     * @param propertiesKey 消息是否為屬性文件中的Key
     */
    public CreditException(String errorCode, String message, boolean propertiesKey) {
        super(message);
        this.setErrorCode(errorCode);
        this.setPropertiesKey(propertiesKey);
    }

    /**
     * 構造一個基本異常.
     *
     * @param errorCode 錯誤編碼
     * @param message   信息描述
     */
    public CreditException(String errorCode, String message, Throwable cause, boolean propertiesKey) {
        super(message, cause);
        this.setErrorCode(errorCode);
        this.setPropertiesKey(propertiesKey);
    }

    /**
     * 構造一個基本異常.
     *
     * @param message 信息描述
     * @param cause   根異常類(可以存入任何異常)
     */
    public CreditException(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 enum PyCreditServiceErrorEnum {
/********************** 基礎異常碼 **********************/ EMPTY_PARAMETERS("10001", "參數為空,請校驗參數"), CHECK_EMPTY_PARAMETERS("10002","請檢查查詢條件,姓名、身份證號以及電話號,參數為空"), CHECK_PHONE("10003","請輸入正確格式手機號"), CHECK_DOCUMENTNO("10004","請輸入正確格式的身份證號碼"), CHECK_NAME("10005","請輸入正確格式的姓名"), CHECK_TIMESTAMP("10006","請輸入時間戳"), CHECK_UMNAME("10007","請輸入UM帳號"), EMPTY_NAME_PHONE("10008","請檢查查詢條件,姓名,身份證號為空"), EMPTY_QUERYTYPE("10009", "請設置查詢類型"), EMPTY_QUERYREASONCODE("10010", "請設置查詢原因碼值"), EMPTY_QUERY_WAY("10011", "請設置查詢方式"), EMPTY_CHANNEL_NAME("10012", "請設置渠道名稱"), EMPTY_QUERY_REASON("10013", "無此查詢原因"), /********************** 業務異常 **********************/ FAILED_UPLOAD_UDMP("20001","雲平台服務失效"), FAILED_CREATE_PDF("20002","生成PDF失敗"), FAILED_UPLOAD_IM("20003","上傳至影像庫失敗"), IDENTITY_NOT_MATCH("20004","身份不匹配"), NOT_FOUND_PDF("20005","無對應文檔ID的PDF文件或文檔已過期,請重新根據姓名身份證號進行查詢"), FAILED_SAVE_PY_RESULT("20006","鵬元結果保存失敗"), /********************** 鵬元服務商接口不可用 **********************/ PY_SYS_ERROR("30001","鵬元服務不可用"), PY_SYS_TIMEOUT("30002","鵬元服務超時"), /********************** 系統異常 **********************/ SYSTEM_ERROR("50000","接口未知異常"); private String code; private String msg; PyCreditServiceErrorEnum(String code, String text) { this.code = code; this.msg = text; } 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; } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM