zoukankan      html  css  js  c++  java
  • 自定义业务异常处理

    要处理什么异常:

    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; } }
  • 相关阅读:
    TFIDF
    奇异值分解(singular value decomposition,SVD)
    (转载)先验分布
    在博客中写公式的两种方法
    linux 下配置JDK JRE ECLIPSE TOMCAT
    tensor decomposition
    Form界面设置只读
    FORM LOV特性
    在Oracle Form中,如何实现自动编号(行号)的功能
    文件夹错误frm41097,frm41053
  • 原文地址:https://www.cnblogs.com/shenkebky/p/8311169.html
Copyright © 2011-2022 走看看