forest
forest copied to clipboard
dubbo服务中,内部异常ForestNetworkException会直接导致序列化崩溃
java.lang.RuntimeException: Serialized class com.dtflys.forest.backend.okhttp3.response.OkHttp3ForestResponse must implement java.io.Serializable
Java field: private com.dtflys.forest.http.ForestResponse com.dtflys.forest.exceptions.ForestNetworkException.response
Java field: private java.lang.Throwable java.lang.Throwable.cause
原因是ForestNetworkException
类有一个字段ForestResponse
, 而ForestResponse
没有实现Serializable
接口
public class ForestNetworkException extends ForestRuntimeException {
private Integer statusCode;
private ForestResponse response;
public ForestNetworkException(String message, Integer statusCode, ForestResponse response) {
super(errorMessage(message, statusCode, response));
this.statusCode = statusCode;
this.response = response;
}
// ...some other...
}
复现方法 a服务调用b服务, b服务使用forest发起请求
Object result = Forest.post(url)
.onError((ex, req, res) -> {
throw new RuntimeException(ex);
})
.execute();
当请求出现异常,进入onError时,如果将内部异常ex作为cause
传入抛出,将出现这个问题,如果不传入,忽略ex。则正常
ForestResponse 不是一个可以序列化的对象,对于dubbo的问题,应该不能直接抛出 ForestNetworkException,或是重新包装成其他类型的异常,但不包含 ForestNetworkException