Okhttp: Issue with throwing some exception before calling proceed in interceptor

Created on 3 Nov 2015  路  5Comments  路  Source: square/okhttp

I want to create interceptor, which returns specific exception in the case of no internet connection is available.
I did something like that:

public class CheckInternet implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        if (!isAnyNetworkConnected())
            throw new NoInternetException();
        Response response;
        try {
            response = chain.proceed(chain.request());
        } catch (SocketTimeoutException | UnknownHostException e) {
            throw new NoInternetException();
        }
        return response;
    }
}

But when I throw exception before calling proceed, OkHttp will fail because HttpEngine inside is created after all interceptors in getResponse() method.

FATAL EXCEPTION: OkHttp Dispatcher
Process: de.wirecard.accept, PID: 24006
java.lang.NullPointerException: Attempt to invoke virtual method 'com.squareup.okhttp.Request com.squareup.okhttp.internal.http.HttpEngine.getRequest()' on a null object reference
at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:175)
at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

Is there any other option how to achieve stopping request with specific exception in onFailure() ?

Most helpful comment

In 2.6.0-SNAPSHOT is fixed.

All 5 comments

Try 2.6.0-SNAPSHOT. Please report back here if that fixes your problem.

In 2.6.0-SNAPSHOT is fixed.

Great!

what do you mean by 2.6.0-SNAPSHOT. I am facing same issue. app crashing on throwing exception inside interceptor

what do i need to change ??
implementation 'com.squareup.retrofit2:retrofit:2.6.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
implementation 'com.squareup.okhttp3:okhttp:4.2.1'
implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'

Was this page helpful?
0 / 5 - 0 ratings