forest icon indicating copy to clipboard operation
forest copied to clipboard

dubbo服务中,内部异常ForestNetworkException会直接导致序列化崩溃

Open timnick-snow opened this issue 2 years ago • 2 comments

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...
}

timnick-snow avatar Nov 17 '22 05:11 timnick-snow

复现方法 a服务调用b服务, b服务使用forest发起请求

        Object result = Forest.post(url)
                .onError((ex, req, res) -> {
                    throw new RuntimeException(ex);
                })
                .execute();

当请求出现异常,进入onError时,如果将内部异常ex作为cause传入抛出,将出现这个问题,如果不传入,忽略ex。则正常

timnick-snow avatar Nov 17 '22 06:11 timnick-snow

ForestResponse 不是一个可以序列化的对象,对于dubbo的问题,应该不能直接抛出 ForestNetworkException,或是重新包装成其他类型的异常,但不包含 ForestNetworkException

mySingleLive avatar Nov 22 '22 03:11 mySingleLive