Issue with throwing some exception before calling proceed in interceptor
See original GitHub issueI 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:
- Created 8 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In 2.6.0-SNAPSHOT is fixed.
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’