okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

Throw exeptions in Interceptor

Open JimmyGoGoGo opened this issue 4 years ago • 1 comments

Beforn in version 3.10.0 :

OKHttp's Interceptor is OK when I try to throw other Exceptions but not specified IOException,normally process to failure callback like what I want.

Now in version 4.10.0:

Other Exceptions throw in Interceptor will be rethrow,and then app crash. What make me confuse is why expcetion should be rethorw, like this code below in file RealCall.kt

 catch (t: Throwable) {
          cancel()
          if (!signalledCallback) {
            val canceledException = IOException("canceled due to $t")
            canceledException.addSuppressed(t)
            responseCallback.onFailure(this@RealCall, canceledException)
          }
//Why rethrow it
          throw t
        }

And this is my code below it crash:

                    //crash
//                    if (code == -1) {
//                        throw SessionException(if (TextUtils.isEmpty(msg)) "session time out" else msg)
//                    } else {
//                        throw ResultException(code, msg)
//                    }
                    //normal
                    throw IOException("safe")

JimmyGoGoGo avatar Oct 22 '22 03:10 JimmyGoGoGo

Dupe of https://github.com/square/okhttp/issues/4198#issuecomment-435635079

Throw some subclass of IOException.

Yes, this is working as intended. Crashes crash.

There are so many potential runtime exceptions and code paths that won't work correctly if Interceptors, EventListeners, Dns, SocketFactory and other extension points can throw any exception. So our rule is IOExceptions are cleanly handled, but runtime exceptions are a crash.

yschimke avatar Oct 22 '22 06:10 yschimke