简述一下背景。线上的应用A导出文件失败,但是对应的任务状态没有设置。经过代码的梳理,大致流程为:
- 创建导出任务
- 执行异步任务:
2.1 文件下载、打包
2.2 调用应用B的Feign接口上传文件 - 更新异步任务状态
在执行2.2这一步时,调用Feign接口内部抛了一个异常:
对应代码是:
照理说,这样导出任务会置为失败:
但是,由于EncodeException的message是null,导致substring()方法调用时抛了个NPE,设置任务状态的代码不会执行。
Exception的message,是允许为null的,但是在Feign的EncodeException代码里却是这样:
public class EncodeException extends FeignException {private static final long serialVersionUID = 1L;/*** @param message the reason for the failure.*/public EncodeException(String message) {super(checkNotNull(message, "message"));}/*** @param message possibly null reason for the failure.* @param cause the cause of the error.*/public EncodeException(String message, Throwable cause) {super(message, checkNotNull(cause, "cause"));}
}
所以这里就很奇怪,在第一个构造器里,message不能为null,但在第二个构造器里,又允许为null。那么对于依赖EncodeException中message内容的代码,仍然要对null做处理,既然如此,为什么第一个构造器允许message为空又何妨?
另一种保持行为一致的方式,可以给第二个构造器加一个默认的message,仅当传入值非空时才使用传入值。
对于这个问题,由于我对Feign整体的了解有限,没有提Pull request而是提了一个issue:Why EncodeException's message is not nullable in 1st constructor but nullable in 2nd constructor?。文中应用A使用io.github.openfeign的是9.5.1,而对于github上主干分支,仍然有类似问题。