question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with throwing some exception before calling proceed in interceptor

See original GitHub issue

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() ?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
Kikjucommented, Nov 4, 2015

In 2.6.0-SNAPSHOT is fixed.

0reactions
AhmadIzazcommented, Oct 27, 2019

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’

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can i have interceptor on throw exception? - JBoss.org
yes, you can just add an interceptor that catches exceptions. i made an ejb3 interceptor for a client some time ago that was...
Read more >
Handle exceptions thrown by a custom okhttp Interceptor in ...
I'm trying to make it work using Kotlin coroutines. The problem is that I'm unable to handle the before mentioned error, since in...
Read more >
HTTP errors and OkHttp interceptor that throws exceptions
One solution we have tried is adding a call to NewRelic#noticeHttpTransaction in our custom interceptor. This works and the errors are reported ...
Read more >
Interceptors - OkHttp
Interceptors are a powerful mechanism that can monitor, rewrite, and retry calls. Here's a simple interceptor that logs the outgoing request and the...
Read more >
Introduction to Spring MVC HandlerInterceptor - Baeldung
4. Spring Handler Interceptor · prehandle() – called before the execution of the actual handler · postHandle() – called after the handler is ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found