Skip to content

Commit 8c2f383

Browse files
authored
Document interceptor throwing modes (#6235)
1 parent a76c40a commit 8c2f383

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

okhttp/src/main/kotlin/okhttp3/Interceptor.kt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,39 @@ import java.util.concurrent.TimeUnit
2222
* Observes, modifies, and potentially short-circuits requests going out and the corresponding
2323
* responses coming back in. Typically interceptors add, remove, or transform headers on the request
2424
* or response.
25+
*
26+
* Implementations of this interface throw [IOException] to signal connectivity failures. This
27+
* includes both natural exceptions such as unreachable servers, as well as synthetic exceptions
28+
* when responses are of an unexpected type or cannot be decoded.
29+
*
30+
* Other exception types cancel the current call:
31+
*
32+
* * For synchronous calls made with [Call.execute], the exception is propagated to the caller.
33+
*
34+
* * For asynchronous calls made with [Call.enqueue], an [IOException] is propagated to the caller
35+
* indicating that the call was canceled. The interceptor's exception is delivered to the current
36+
* thread's [uncaught exception handler][Thread.UncaughtExceptionHandler]. By default this
37+
* crashes the application on Android and prints a stacktrace on the JVM. (Crash reporting
38+
* libraries may customize this behavior.)
39+
*
40+
* A good way to signal a failure is with a synthetic HTTP response:
41+
*
42+
* ```
43+
* @Throws(IOException::class)
44+
* override fun intercept(chain: Interceptor.Chain): Response {
45+
* if (myConfig.isInvalid()) {
46+
* return Response.Builder()
47+
* .request(chain.request())
48+
* .protocol(Protocol.HTTP_1_1)
49+
* .code(400)
50+
* .message("client config invalid")
51+
* .body("client config invalid".toResponseBody(null))
52+
* .build()
53+
* }
54+
*
55+
* return chain.proceed(chain.request())
56+
* }
57+
* ```
2558
*/
2659
fun interface Interceptor {
2760
@Throws(IOException::class)

0 commit comments

Comments
 (0)