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.

RuntimeException thrown in interceptor never notifies Call

See original GitHub issue

If an okhttp3.Interceptor throws an exception, the suspend function in a Retrofit interface never completes (neither returning a value or throwing an exception).

A sample project demonstrating the issue with a simple JUnit test can be found here: https://github.com/robfletcher/retrofit-coroutines-hang

The real situation when I’ve encountered this is when an SSL handshake fails.

FWIW the same behavior is seen using https://github.com/JakeWharton/retrofit2-kotlin-coroutines-adapter with Retrofit 2.5.0

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:7
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

7reactions
Hodkinsoncommented, Sep 29, 2019

Just adding a workaround here in case it helps. Actually in my case even IOExceptions are not propagating back. Retrofit 2.6.2, okhttp 4.2.0

I wanted to deal with this at point of call rather than add an UncaughtExceptionHandler.

So the workaround was to add an interceptor which catches any IOExceptions, and returns a blank response with a magic status code.

private val errorInterceptor = object : Interceptor {
        override fun intercept(chain: Interceptor.Chain): okhttp3.Response {
            return try {
                chain.proceed(chain.request())
            } catch (e: IOException) {
                okhttp3.Response.Builder()
                    .code(418)
                    .request(chain.request())
                    .body(object: ResponseBody() {
                        override fun contentLength() = 0L
                        override fun contentType(): MediaType? = null
                        override fun source(): BufferedSource = Buffer()
                    })
                    .message(e.message ?: e.toString())
                    .protocol(Protocol.HTTP_1_1)
                    .build()
            }
        }
    }

This could be combined with a Result object like in the answer here: https://stackoverflow.com/questions/57625272/retrofit-2-6-0-custom-coroutines-calladapterfactory where the status code or message is checked and a NetworkError returned.

6reactions
Nghicvcommented, Mar 28, 2020

Hi @robfletcher, are there any updates on this issue?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handle exceptions thrown by a custom okhttp Interceptor in ...
Actually coroutine's suspend functions throws exception when dealing with network calls. In my experience, you can follow 2 approaches. 1. wrap ...
Read more >
Class PersistenceExceptionTranslationInterceptor - Spring
Delegates to the given PersistenceExceptionTranslator to translate a RuntimeException thrown into Spring's DataAccessException hierarchy (if appropriate).
Read more >
TransactionalException (Java(TM) EE 7 Specification APIs)
public class TransactionalException extends RuntimeException. The TransactionalException thrown from the Transactional interceptors implementation contains ...
Read more >
3. Resolved and Known Issues Red Hat JBoss Enterprise ...
ERROR [org.jboss.remoting.handler-errors] Close handler threw an exception: ... The channel is now closed when Context.close() is called, even if other ...
Read more >
Map - Hazelcast Documentation
ConcurrentMap operations never throw a java.util. ... it is propagated to the original IMap.put() or IMap.remove() call in the form of RuntimeException ....
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