用Assert(断言)封装异常,让代码更优雅(附项目源码)

有关Assert断言大家并不陌生,我们在做单元测试的时候,看业务事务复合预期,我们可以通过断言来校验,断言常用的方法如下:

public class Assert { /** * 结果 = 预期 则正确 */ static public void assertEquals(Object expected, Object actual); /** * 结果 != 预期 则正确 */ static public void assertNotEquals(Object unexpected, Object actual); /** * condition == true 则正确 */ static public void assertTrue(boolean condition); /** * condition == false 则正确 */ static public void assertFalse(boolean condition); /** * 永远是错误 */ static public void fail(); /** * 结果不为空 则正确 */ static public void assertNotNull(Object object); /** * 结果为空 则正确 */ static public void assertNull(Object object); /** * 两个对象引用 相等 则正确(上面相当于equels 这里类似于使用“==”比较两个对象) */ static public void assertSame(Object expected, Object actual); /** * 两个对象引用 不相等 则正确 */ static public void assertNotSame(Object unexpected, Object actual); /** * 两个数组相等 则正确 */ public static void assertArrayEquals(Object[] expecteds, Object[] actuals); /** * 这个单独介绍 具体参考博客:https://www.cnblogs.com/qdhxhz/p/13684458.html */ public static void assertThat(T actual, Matcher matcher); };

使用断言能让我们编码看去更加的清爽,比如:
@Test public void test1() { Order order = orderDao.selectById(orderId); Assert.notNull(order, "订单不存在。"); }@Test public void test2() { // 另一种写法 Order order = orderDao.selectById(orderId); if (order == null) { throw new IllegalArgumentException("订单不存在。"); } }

这两种方式一对比,是不是明显感觉第一种更优雅,第二种写法则是相对丑陋的 if {...} 代码块。那么 神奇的 Assert.notNull() 背后到底做了什么呢?
下面是 Assert 的部分源码:
public abstract class Assert { public Assert() { }public static void notNull(@Nullable Object object, String message) { if (object == null) { throw new IllegalArgumentException(message); } } }

可以看到,Assert 其实就是帮我们把 if {...} 封装了一下,是不是很神奇。虽然很简单,但不可否认的是编码体验至少提升了一个档次。
那么我们是不是可以模仿Assert,也写一个自定义断言类,不过断言失败后抛出的异常不是IllegalArgumentException 这些内置异常,而是我们自己定义的异常。
下面让我们来尝试一下。
public interface Assert { /** * 创建异常 * @param args * @return */ BaseException newException(Object... args); /** * 创建异常 * @param t * @param args * @return */ BaseException newException(Throwable t, Object... args); /** * 断言对象 obj 非空。如果对象 obj 为空,则抛出异常 * * @param obj 待判断对象 */ default void assertNotNull(Object obj) { if (obj == null) { throw newException(obj); } }/** * 断言对象 obj 非空。如果对象 obj 为空,则抛出异常 * 异常信息 message 支持传递参数方式,避免在判断之前进行字符串拼接操作 * * @param obj 待判断对象 * @param args message占位符对应的参数列表 */ default void assertNotNull(Object obj, Object... args) { if (obj == null) { throw newException(args); } } }


  1. 这里只给出Assert接口的部分源码,更多断言方法请参考项目的源码。
  2. BaseException 是所有自定义异常的基类。
  3. 在接口中定义默认方法是Java8的新语法。
上面的Assert断言方法是使用接口的默认方法定义的,然后有没有发现当断言失败后,抛出的异常不是具体的某个异常,而是交由2个newException接口方法提供。
因为业务逻辑中出现的异常基本都是对应特定的场景,比如根据用户id获取用户信息,查询结果为null,此时抛出的异常可能为UserNotFoundException,并且有特
定的异常码(比如7001)和异常信息“用户不存在”。所以具体抛出什么异常,有Assert的实现类决定。
看到这里,你可能会有会有疑问,按照上面的做法,那不是有多少个异常情况,我都要去实现Assert接口,然后在写定义等量的断言类和异常类,这显然是反人类的,
这也没想象中高明嘛。别急,且听我细细道来。

一、善解人意的Enum 自定义异常BaseException有2个属性,即code、message,这样一对属性,有没有想到什么类一般也会定义这2个属性?
没错,就是枚举类。且看我如何将 Enum 和 Assert 结合起来,相信我一定会让你眼前一亮。如下:
public interface IResponseEnum { int getCode(); String getMessage(); }

我们自定一个业务异常类
/** * 业务异常 * 业务处理时,出现异常,可以抛出该异常 * */ public class BusinessException extendsBaseException {private static final long serialVersionUID = 1L; public BusinessException(IResponseEnum responseEnum, Object[] args, String message) { super(responseEnum, args, message); }public BusinessException(IResponseEnum responseEnum, Object[] args, String message, Throwable cause) { super(responseEnum, args, message, cause); } }

业务异常断言实现类,它同时继承2个接口,一个是Assert接口,一个是IResponseEnum接口
public interface BusinessExceptionAssert extends IResponseEnum, Assert {@Override default BaseException newException(Object... args) { String msg = MessageFormat.format(this.getMessage(), args); return new BusinessException(this, args, msg); }@Override default BaseException newException(Throwable t, Object... args) { String msg = MessageFormat.format(this.getMessage(), args); return new BusinessException(this, args, msg, t); }}

业务异常枚举
/** *业务异常枚举 */ @Getter @AllArgsConstructor public enum BusinessResponseEnum implements BusinessExceptionAssert {USER_NOT_FOUND(6001, "未查询到用户信息"),ORDER_NOT_FOUND(7001, "未查询到订单信息"),; /** * 返回码 */ private int code; /** * 返回消息 */ private String message; }

这样如果是业务异常,就都可以在BusinessResponseEnum中定义一个新枚举对象就可以了。以后每增加一种异常情况,只需增加一个枚举实例即可,再也不用每一种异常
都定义一个异常类了。接下来看使用如下:
/** * 查询用户信息 */ public User queryDetail(Integer userId) { final User user = this.getById(userId); // 校验非空 BusinessResponseEnum.USER_NOT_FOUND.assertNotNull(user); return user; }

若不使用断言,代码可能如下:
/** * 查询用户信息 */ public User queryDetail(Integer userId) { final User user = this.getById(userId); if (user == null) { throw new UserNotFoundException(); // 或者这样 throw new BusinessException(6001, "未查询到用户信息"); } return user; }

使用枚举类结合(继承)Assert,只需根据特定的异常情况定义不同的枚举实例,如上面的USER_NOT_FOUND、ORDER_NOT_FOUND,就能够针对不同情况抛出特定的异常
(这里指携带特定的异常码和异常消息),这样既不用定义大量的异常类,同时还具备了断言的良好可读性。

二、验证统一异常处理 在throw抛出异常后,我们就可以统一去处理异常,如果有必要我们可以加入异常日志存入数据库中,有助于后续排查问题。
/** *全局异常处理器 * * @author xub * @date 2022/2/28 上午10:52 */ @Slf4j @Component @ControllerAdvice @ConditionalOnWebApplication public class ViewExceptionResolver { /** * 生产环境 */ private final static String ENV_PROD = "prod"; @Autowired private UnifiedMessageSource unifiedMessageSource; /** * 当前环境 */ @Value("${spring.profiles.active}") private String profile; /** * 获取国际化消息 * * @param e 异常 * @return */ public String getMessage(BaseException e) { String code = "response." + e.getResponseEnum().toString(); String message = unifiedMessageSource.getMessage(code, e.getArgs()); if (message == null || message.isEmpty()) { return e.getMessage(); }return message; }/** * 业务异常 * * @param e 异常 * @return 异常结果 */ @ExceptionHandler(value = https://www.it610.com/article/BusinessException.class) @ResponseBody public CommandResult handleBusinessException(BaseException e) { log.error(e.getMessage(), e); return CommandResult.ofFail(e.getResponseEnum().getCode(), getMessage(e)); }/** * 自定义异常 * * @param e 异常 * @return 异常结果 */ @ExceptionHandler(value = BaseException.class) @ResponseBody public CommandResult handleBaseException(BaseException e) { log.error(e.getMessage(), e); return CommandResult.ofFail(e.getResponseEnum().getCode(), getMessage(e)); }/** * 未定义异常 * * @param e 异常 * @return 异常结果 */ @ExceptionHandler(value = Exception.class) @ResponseBody public CommandResult handleException(Exception e) { log.error(e.getMessage(), e); if (ENV_PROD.equals(profile)) { // 当为生产环境, 不适合把具体的异常信息展示给用户, 比如数据库异常信息. int code = CommonResponseEnum.SERVER_ERROR.getCode(); BaseException baseException = new BaseException(CommonResponseEnum.SERVER_ERROR); String message = getMessage(baseException); return CommandResult.ofFail(code, message); }return CommandResult.ofFail(CommonResponseEnum.SERVER_ERROR.getCode(), e.getMessage()); } }


三、测试验证 这里请求地址,业务id=10数据库是不存在这个用户,所以会报错。
localhost:8085/user/getUserInfo?userId=10

用Assert(断言)封装异常,让代码更优雅(附项目源码)
文章图片
达到了预期效果。

四、总结 使用 断言枚举类 相结合的方式,再配合统一异常处理,基本大部分的异常都能够被捕获。为什么说大部分异常,因为当引入 spring cloud security 后,还会
有认证/授权异常,网关的服务降级异常、跨模块调用异常、远程调用第三方服务异常等,这些异常的捕获方式与本文介绍的不太一样,不过限于篇幅,这里不做详细说明,
以后会有单独的文章介绍。
项目地址:用Assert(断言)封装异常,让代码更优雅(附项目源码)

感谢
这篇文章给自己提供了很好的思路,基本上按照这个思路往下写的
【用Assert(断言)封装异常,让代码更优雅(附项目源码)】统一异常处理介绍及实战:https://www.jianshu.com/p/3f3d9e8d1efa

    推荐阅读