断言+异常处理类,代码更简洁了

简介: 断言+异常处理类,代码更简洁了
  • 背景
  • 业务异常处理示例
  • 附上代码

背景

软件开发过程中,不可避免的是需要处理各种异常,所以代码中就会出现大量的 try {...} catch {...} finally {...} 代码块,不仅有大量的冗余代码,而且还影响代码的可读性。

另一个就是面对业务异常的情况,我们经常需要将业务异常结果组装成统一的信息返回给前端进行提示。

假如我们在每个接口中都去包装异常信息进行返回就会让代码变得很冗余且混乱。在我司的实际项目开发过程中,我们会巧用断言去简化代码。

基于 Spring Boot + MyBatis Plus + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能。

项目地址:https://github.com/YunaiV/ruoyi-vue-pro

业务异常处理示例

假设我们定义的标准接口响应实体为 ApiResult:

@Data
 @Builder
 @NoArgsConstructor
 @AllArgsConstructor
 public class ApiResult<T> implements Serializable {
     private static final long serialVersionUID = 411731814484355577L;
     private int responseCode;
     private String responseMsg;
     private boolean isSuccess;
     private T data;
     public String toString() {
         return "ApiResult(responseCode=" + this.getResponseCode() + ", responseMsg=" + this.getResponseMsg() + ", isSuccess=" + this.isSuccess() + ", data=" + this.getData() + ")";
     }
 }

那么我们接口处理业务逻辑时代码就会变成这样,看起来非常多代码:

public ApiResult cancelService(@PathVariable Long serviceOrderId){
     ServiceOrder serviceOrder = serviceOrderMapper.selectByPrimaryKey(serviceOrderId);
     ApiResult result = new ApiResult<>();
     if (ObjectUtil.isNull(serviceOrder)) {
         result.setSuccess(false);
         result.setResponseCode(ErrorCodeEnum.FAIL.getCode());
         result.setResponseMsg("查无此服务单");
         return result;
     }
     if(serviceOrder.getOrderStatus().equals(cancelOrderStatus)){
         result.setSuccess(false);
         result.setResponseCode(ErrorCodeEnum.FAIL.getCode());
         result.setResponseMsg("已取消的服务单不允许再次取消");
         return result;
     }
     if(serviceOrder.getSortOrderId() != null){
         result.setSuccess(false);
         result.setResponseCode(ErrorCodeEnum.FAIL.getCode());
         result.setResponseMsg("已配置物料的服务单不允许取消");
         return result;
     }
     // ...other check
     // ...do something
     return result;
 }

然后在上面这个代码基础上,我们可以观察到,里面其实有非常多的重复代码,完全可以把它们装到 ApiResult 里面。

这也是我看到很多开源框架的处理方式(PS:所以我第一个自己写的框架也是这么处理的)

在原 ApiResult 实体中增加一些公用的处理方法:

public static ApiResult<String> success() {
     return success("success");
 }
 public static <T> ApiResult<T> success(T data) {
     return (new ApiResult()).setResponseCode(0).setResponseMsg("操作成功").setSuccess(true).setData(data);
 }
 public static ApiResult<String> fail() {
     return fail(-1);
 }
 public static ApiResult<String> fail(int code) {
     return fail(code, "fail");
 }
 public static <T> ApiResult<T> fail(T data) {
     return fail(-1, data);
 }
 public static <T> ApiResult<T> fail(int code, T data) {
     return (new ApiResult()).setResponseCode(code).setResponseMsg("操作失败").setSuccess(false).setData(data);
 }
 public static <T> ApiResult<T> success(int code, String message, T data) {
     return (new ApiResult()).setResponseCode(code).setResponseMsg(message).setSuccess(true).setData(data);
 }
 public static <T> ApiResult<T> fail(int code, String message, T data) {
     return (new ApiResult()).setResponseCode(code).setResponseMsg(message).setSuccess(false).setData(data);
 }

然后业务逻辑处理就变成这样了,看起来还不错是不是:

/**
  * 取消服务单(不用断言)
  */
 public ApiResult cancelService(Long serviceOrderId){
     ServiceOrder serviceOrder = serviceOrderMapper.selectByPrimaryKey(serviceOrderId);
     ApiResult result = new ApiResult<>();
     if (ObjectUtil.isNull(serviceOrder)) {
         result = ApiResult.fail(ErrorCodeEnum.FAIL.getCode(), "查无此服务单");
         return result;
     }
     if(serviceOrder.getOrderStatus().equals(cancelOrderStatus)){
         result = ApiResult.fail(ErrorCodeEnum.FAIL.getCode(), "已取消的服务单不允许再次取消");
         return result;
     }
     if(serviceOrder.getSortOrderId() != null){
         result = ApiResult.fail(ErrorCodeEnum.FAIL.getCode(), "已配置物料的服务单不允许取消");
         return result;
     }
     // ...other check
     // ...do something
     return result;
 }

但是我们可以用异常处理类+断言处理得更加简化。

增加异常处理类:

@Slf4j
 @ControllerAdvice
 public class GlobalExceptionHandler {
     @ExceptionHandler(value = BusinessException.class)
     @ResponseBody
     public ResponseBean businessExceptionHandler(BusinessException e) {
         log.info("business error : {}",e.getMessage(),e);
         if (e.getCode() == -1) {
             return ResponseBean.error(ApiCode.SERVICE_ERROR.getValue(), ApiCode.SERVICE_ERROR.getMessage());
         }
         return ResponseBean.error(e.getCode(), e.getMessage());
     }
 }

增加异常类 BusinessException:

/**
  * 业务异常,异常信息会返回到前端展示给用户
  *
  * @date 2020/12/15 14:18
  */
 public class BusinessException extends RuntimeException {
     private static final long serialVersionUID = -5770538329754222306L;
     private int code = 1;
     private Level level;
     public BusinessException(int code, String message, Throwable cause) {
         super(message, cause);
         this.code = code;
     }
     public BusinessException(String message) {
         super(message);
     }
     public BusinessException(Level level, String message) {
         super(message);
         this.level = level;
     }
     public BusinessException(Throwable cause) {
         super(cause);
     }
     public BusinessException(int code, String message) {
         super(message);
         this.code = code;
     }
     public int getCode() {
         return this.code;
     }
     public final Level getLevel() {
         return this.level;
     }
 }

增加断言工具类 AssertUtil:

public class AssertUtil extends cn.com.bluemoon.common.web.exception.AssertUtil  {
     public AssertUtil() {
     }
     /**
      * 服务调用异常
      * @param expression
      * @param message
      */
     public static void isTrueServiceInvoke(boolean expression, String message) {
         if (!expression) {
             throw new ServiceInvokeException(message);
         }
     }
     /**
      * 抛出异常(默认错误1000)
      * @param message
      */
     public static void businessInvalid(String message) {
         throw new BusinessException(ApiCode.SERVICE_ERROR.getValue(), message);
     }
     /**
      * 表达式为真即抛出异常(默认错误1000)
      *
      * @param expression
      * @param message
      */
     public static void businessInvalid(boolean expression, String message) {
         if (expression) {
             throw new BusinessException(ApiCode.SERVICE_ERROR.getValue(), message);
         }
     }
     /**
      * 表达式为真即抛出异常
      *
      * @param expression
      * @param message
      */
     public static void businessInvalid(boolean expression, int code, String message) {
         if (expression) {
             throw new BusinessException(code, message);
         }
     }
 }

最后优化的结果:

/**
  * 取消服务单
  */
 public ApiResult cancelService(@PathVariable Long serviceOrderId){
     ServiceOrder serviceOrder = serviceOrderMapper.selectByPrimaryKey(serviceOrderId);
     AssertUtil.businessInvalid(ObjectUtil.isNull(serviceOrder),"查无此服务单");
     AssertUtil.businessInvalid(serviceOrder.getOrderStatus().equals(cancelOrderStatus),"查无此服务单");
     AssertUtil.businessInvalid(serviceOrder.getSortOrderId() != null,"查无此服务单");
     // ...other check
     // ...do something
     return ApiResult.success();
 }

最后,我们可以看到我们的接口由 19 行的业务检查代码简化到了 3 行。这只是单接口的情况下,在业务多且复杂的情况下能给我们节省更多的开发时间,把精力集中在核心业务上。

基于微服务的思想,构建在 B2C 电商场景下的项目实战。核心技术栈,是 Spring Boot + Dubbo 。未来,会重构成 Spring Cloud Alibaba 。

项目地址:https://github.com/YunaiV/onemall

附上代码

统一异常处理类:

/**
  * 统一异常处理
  */
 @Slf4j
 @ControllerAdvice
 public class GlobalExceptionHandler {
     @ExceptionHandler(value = AssertException.class)
     @ResponseBody
     public ResponseBean bootExceptionHandler(AssertException e) {
         ApiCode apiCode = ApiCode.getObjectByValue(e.getCode());
         log.error("business error : {}", e.getMessage(), e);
         if (e.getCode() == -1) {
             return ResponseBean.error(ApiCode.SERVICE_ERROR.getValue(), ApiCode.SERVICE_ERROR.getMessage());
         }
         return ResponseBean.error(apiCode.getValue(), e.getMessage());
     }
     @ExceptionHandler(value = com.alibaba.fastjson.JSONException.class)
     public ResponseBean alibabaJsonExceptionHandler(com.alibaba.fastjson.JSONException e) {
         ResponseBean response = new ResponseBean(false, ApiCode.PARAM_FORMAT_INCORR.getValue(), ApiCode.PARAM_FORMAT_INCORR.getMessage() + e.getMessage(), null);
         log.error("1102", e);
         return response;
     }
     @ExceptionHandler(value = JSONException.class)
     @ResponseBody
     public ResponseBean jsonExceptionHandler(JSONException e) {
         ResponseBean response = new ResponseBean(false, ApiCode.PARAM_FORMAT_INCORR.getValue(), ApiCode.PARAM_FORMAT_INCORR.getMessage() + e.getMessage(), null);
         log.error(ApiCode.PARAM_FORMAT_INCORR.getValue() + "", e);
         return response;
     }
     @ExceptionHandler(value = JsonParseException.class)
     @ResponseBody
     public ResponseBean jsonParseExceptionHandler(JsonParseException e) {
         ResponseBean response = new ResponseBean(false, ApiCode.PARAM_FORMAT_INCORR.getValue(), String.format(ApiCode.PARAM_FORMAT_INCORR.getMessage() + ":%s", e.getMessage()), null);
         log.error(ApiCode.PARAM_FORMAT_INCORR.getValue() + "", e);
         return response;
     }
     @ExceptionHandler(value = Exception.class)
     @ResponseBody
     public ResponseBean exceptionHandler(Exception e) {
         ResponseBean response = new ResponseBean(false, ApiCode.SERVICE_ERROR.getValue(), ApiCode.SERVICE_ERROR.getMessage(), null);
         log.error(ApiCode.SERVICE_ERROR.getValue() + "", e);
         return response;
     }
     @ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
     @ResponseBody
     public ResponseBean exceptionHandle(MethodArgumentTypeMismatchException e) {
         ResponseBean response = new ResponseBean(false, ApiCode.PARAM_FORMAT_INCORR.getValue(), String.format(ApiCode.PARAM_FORMAT_INCORR.getMessage() + ":%s", e.getMessage()), null);
         log.error(ApiCode.PARAM_FORMAT_INCORR.getValue() + "", e);
         return response;
     }
     @ExceptionHandler(value = WebException.class)
     @ResponseBody
     public ResponseBean exceptionHandler(WebException e) {
         ResponseBean response = new ResponseBean(e.getIsSuccess(), e.getResponseCode(), e.getResponseMsg(), null);
         log.error(e.getResponseCode() + "", e);
         return response;
     }
     @ExceptionHandler(value = IllegalArgumentException.class)
     @ResponseBody
     public ResponseBean exceptionHandler(IllegalArgumentException e) {
         log.error("illegal request : {}", e.getMessage(), e);
         return ResponseBean.error(ApiCode.PARAM_INVALID.getValue(), ApiCode.PARAM_INVALID.getMessage());
     }
     @ExceptionHandler(value = ServiceInvokeException.class)
     @ResponseBody
     public ResponseBean exceptionHandler(ServiceInvokeException e) {
         log.error("serviceInvoke error request : {}", e.getMessage(), e);
         return ResponseBean.error(ApiCode.SERVICE_ERROR.getValue(), ApiCode.SERVICE_ERROR.getMessage());
     }
     @ExceptionHandler(value = BusinessException.class)
     @ResponseBody
     public ResponseBean businessExceptionHandler(BusinessException e) {
         log.info("business error : {}",e.getMessage(),e);
         if (e.getCode() == -1) {
             return ResponseBean.error(ApiCode.SERVICE_ERROR.getValue(), ApiCode.SERVICE_ERROR.getMessage());
         }
         return ResponseBean.error(e.getCode(), e.getMessage());
     }
     @ResponseBody
     @ExceptionHandler(MethodArgumentNotValidException.class)
     public ResponseBean  exceptionHandler(MethodArgumentNotValidException e) {
         log.info("req params error", e);
         String message = e.getBindingResult().getFieldError().getDefaultMessage();
         if (StringUtils.isNotBlank(message) && !"不能为空".equals(message)) {
             return ResponseBean.error(ApiCode.PARAM_INVALID.getValue(), message);
         }
         return ResponseBean.error(ApiCode.PARAM_INVALID.getValue(), ApiCode.PARAM_INVALID.getMessage());
     }
     @ExceptionHandler(value = TokenErrorException.class)
     @ResponseBody
     public ResponseBean tokenErrorExceptionHandler(TokenErrorException e) {
         log.info("登录失效 : {}",e.getMessage(),e);
         return ResponseBean.error(ApiCode.SERVICE_ERROR.getValue(), "登录已失效,请重新登录!");
     }
     @ExceptionHandler(value = ServiceException.class)
     @ResponseBody
     public ResponseBean businessExceptionHandler(ServiceException e) {
         log.info("service error : {}",e.getMessage(),e);
         return ResponseBean.error(ApiCode.SERVICE_ERROR.getValue(), e.getMessage());
     }
 }

异常情况枚举,仅作参考:

public enum ErrorCodeEnum implements EnumBase{
     FAIL(-1, "网络异常,请稍后再试"),
     SUCCESS(0, "请求成功"),
     MAX_UPLOAD_SIZE_ERROR(1000, "上传文件不能超过20M"),
     SERVICE_BUSY_ERROR(1000, "服务器正在繁忙,请稍后再试哦~"),
     REQUEST_PARAMS_FAIL(1001, "参数错误"),
     USER_NOT_LOGIN(1002, "用户未登录,请重新登录"),
     USER_HAS_EXIST_LOGIN(1007, "用户已经存在,请检查!"),
     USER_CODE_NOT_EXIST(1008, "用户编码不存在,请检查!"),
     REQUEST_PARAMS_FORMAT_ERROR(1102, "请求参数格式异常"),
     PASSWORD_SAFETY_ERROE(2204, "密码不符合安全规则,请通过忘记密码重新设置8-18位数字+字母组合密码"),
     TOKEN_EXPIRED(2301, "token过期"),
     TOKEN_ERROR(2302, "token验证失败"),
     INTERFACE_ERROR(10000, "接口服务器异常");
     private final int code;
     private final String msg;
     ErrorCodeEnum(int code, String msg) {
         this.code = code;
         this.msg = msg;
     }
     @Override
     public int getCode() {
         return this.code;
     }
     @Override
     public String getMsg() {
         return this.msg;
     }
 }


相关文章
|
数据采集 移动开发 文字识别
服务阿里 9 个APP|揭秘新奥创升级的质量演变
新奥创技术体系,是手机淘宝端搭载着星环中台的整个商业化研发体系,孵化出的面对无线电商领域的技术体系。过去一年在手淘完成了下单、详情、购物车三大业务域的改造,接下来还会在订单、手淘导购等领域进行技术升级。目前新奥创已经接入阿里内的9个 App,逐步成为阿里集团无线领域电商系的技术解决方案。 本文主要围绕新奥创技术体系的升级,剖析架构升级对测试保障带来的新的转变,也是新的机遇。
6551 0
服务阿里 9 个APP|揭秘新奥创升级的质量演变
|
数据可视化 API 数据库
低代码是什么?2025低代码技术详解:平台分类、用户群体与发展趋势分析
低代码(Low Code)是一种通过可视化工具和预构建组件,以少量或无代码快速开发应用的技术。2014年Forrester定义其为“用最少手工编码快速开发并部署应用的技术”,随后Gartner推广aPaaS/iPaaS概念推动其发展。
1210 32
|
3月前
|
人工智能 自然语言处理 数据可视化
行业观察|Vibe‑Modeling浪潮之下:国产建筑AI的现实路径与取舍思考
海外AEC赛道提出Vibe‑Modeling,把AI建筑的目标锚定在“读懂设计意图、输出可编辑参数化三维几何体”。但国内建筑AI并没有简单复刻这条技术路线。本文结合行业观察与实测,以ADAI作为国内代表样本,对比海外产品技术范式,剖析国内建筑AI选择“图像层意图约束”这条路线背后的业务现实、工程约束,同时讨论从图像渲染走向几何生成,国内赛道需要跨越哪些硬核障碍。
行业观察|Vibe‑Modeling浪潮之下:国产建筑AI的现实路径与取舍思考
|
6月前
|
运维 Cloud Native Android开发
安卓苹果云手机别乱买!2026选错直接多花冤枉钱
安卓云手机基于ARM虚拟化,支持ROOT、群控、多开、脚本及虚拟定位,成本低、兼容广、风控强;苹果云手机受限于私有架构,难群控、不兼容安卓应用、价格高30%–50%,仅适合单账号轻度挂机。2026年安卓ARM已成主流。
1013 0
|
8月前
|
机器学习/深度学习 人工智能 缓存
《不花一分钱,让你的QClaw在Mac上跑得比云端还快》
本文针对多数Mac用户用x86思维运行QClaw、严重浪费M系列芯片性能的普遍痛点,基于一个月上百次的实测调试,拆解了苹果硅统一内存架构下QClaw的性能优化底层逻辑。文章详细分享了不同内存容量的黄金分配比例、Metal与神经网络引擎的精准调度方法、模型加载与缓存优化技巧、多Agent协同的资源分配策略,同时给出了M1/M2/M3不同代芯片的差异化优化方案。通过这套零成本设置,可将QClaw本地推理速度提升至默认的7倍,实现流畅高效的端侧AI体验。
756 0
WAT测试介绍
本文只要介绍芯片WAT的测试环境,测试结构,测试机台,测试标准,测试项目以及测试的意义。
10950 0
WAT测试介绍
|
数据采集 存储 SQL
连载:阿里巴巴大数据实践—数据开发平台
介绍MaxCompute和阿里巴巴内部基于MaxCompute的大数据开发套件,并对在数据开发过程中经常遇到的问题和相关解决方案进行介绍。
8659 0
连载:阿里巴巴大数据实践—数据开发平台
|
机器学习/深度学习 缓存 自然语言处理
更加灵活、经济、高效的训练——新一代搜推广稀疏大模型训练范式GBA
近日,阿里巴巴在国际顶级机器学习会议NeurIPS 2022上发表了新的自研训练模式 Gloabl Batch gradients Aggregation (GBA,论文链接:https://arxiv.org/abs/2205.11048),由阿里妈妈事业部搜索广告团队和智能引擎事业部XDL训练引擎团队联合探索和研发。GBA的提出对阿里巴巴搜推广稀疏模型的训练范式带来了架构性的跨越式升级。本文将从GBA的设计思路、收敛性分析及工程实现等方面展开介绍,欢迎阅读交流。
更加灵活、经济、高效的训练——新一代搜推广稀疏大模型训练范式GBA