mirror of
https://github.com/square/okhttp.git
synced 2026-01-14 07:22:20 +03:00
Merge pull request #5503 from square/jwilson.0926.unchecked_314x
Cancel calls on unexpected exceptions (3.14.x branch)
This commit is contained in:
@@ -517,11 +517,12 @@ public final class InterceptorTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* When an interceptor throws an unexpected exception, asynchronous callers are left hanging. The
|
||||
* When an interceptor throws an unexpected exception, asynchronous calls are canceled. The
|
||||
* exception goes to the uncaught exception handler.
|
||||
*/
|
||||
private void interceptorThrowsRuntimeExceptionAsynchronous(boolean network) throws Exception {
|
||||
addInterceptor(network, chain -> { throw new RuntimeException("boom!"); });
|
||||
RuntimeException boom = new RuntimeException("boom!");
|
||||
addInterceptor(network, chain -> { throw boom; });
|
||||
|
||||
ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
|
||||
client = client.newBuilder()
|
||||
@@ -531,9 +532,15 @@ public final class InterceptorTest {
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.build();
|
||||
client.newCall(request).enqueue(callback);
|
||||
Call call = client.newCall(request);
|
||||
call.enqueue(callback);
|
||||
RecordedResponse recordedResponse = callback.await(server.url("/"));
|
||||
assertThat(recordedResponse.failure)
|
||||
.hasMessage("canceled due to java.lang.RuntimeException: boom!");
|
||||
assertThat(recordedResponse.failure).hasSuppressedException(boom);
|
||||
assertThat(call.isCanceled()).isTrue();
|
||||
|
||||
assertThat(executor.takeException().getMessage()).isEqualTo("boom!");
|
||||
assertThat(executor.takeException()).isEqualTo(boom);
|
||||
}
|
||||
|
||||
@Test public void applicationInterceptorReturnsNull() throws Exception {
|
||||
|
||||
@@ -179,6 +179,14 @@ final class RealCall implements Call {
|
||||
} else {
|
||||
responseCallback.onFailure(RealCall.this, e);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
cancel();
|
||||
if (!signalledCallback) {
|
||||
IOException canceledException = new IOException("canceled due to " + t);
|
||||
canceledException.addSuppressed(t);
|
||||
responseCallback.onFailure(RealCall.this, canceledException);
|
||||
}
|
||||
throw t;
|
||||
} finally {
|
||||
client.dispatcher().finished(this);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user