mirror of
https://github.com/square/okhttp.git
synced 2025-11-24 18:41:06 +03:00
update Logging Interceptor to log connection failures
This commit is contained in:
@@ -207,7 +207,13 @@ public final class HttpLoggingInterceptor implements Interceptor {
|
||||
}
|
||||
|
||||
long startNs = System.nanoTime();
|
||||
Response response = chain.proceed(request);
|
||||
Response response;
|
||||
try {
|
||||
response = chain.proceed(request);
|
||||
} catch (Exception e) {
|
||||
logger.log("<-- HTTP FAILED: " + e);
|
||||
throw e;
|
||||
}
|
||||
long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
|
||||
|
||||
ResponseBody responseBody = response.body();
|
||||
|
||||
@@ -16,9 +16,12 @@
|
||||
package okhttp3.logging;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
import okhttp3.Dns;
|
||||
import okhttp3.HttpUrl;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
@@ -641,6 +644,29 @@ public final class HttpLoggingInterceptorTest {
|
||||
.assertNoMoreLogs();
|
||||
}
|
||||
|
||||
@Test public void connectFail() throws IOException {
|
||||
setLevel(Level.BASIC);
|
||||
client = new OkHttpClient.Builder()
|
||||
.dns(new Dns() {
|
||||
@Override public List<InetAddress> lookup(String hostname) throws UnknownHostException {
|
||||
throw new UnknownHostException("reason");
|
||||
}
|
||||
})
|
||||
.addInterceptor(applicationInterceptor)
|
||||
.build();
|
||||
|
||||
try {
|
||||
client.newCall(request().build()).execute();
|
||||
fail();
|
||||
} catch (UnknownHostException expected) {
|
||||
}
|
||||
|
||||
applicationLogs
|
||||
.assertLogEqual("--> GET " + url + " http/1.1")
|
||||
.assertLogEqual("<-- HTTP FAILED: java.net.UnknownHostException: reason")
|
||||
.assertNoMoreLogs();
|
||||
}
|
||||
|
||||
private Request.Builder request() {
|
||||
return new Request.Builder().url(url);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user