Skip to content

Commit

Permalink
Fix #1180 OkHttp non-200 response not closed
Browse files Browse the repository at this point in the history
Summary:
OkHttp has added an error message in cases where the reponse body is not closed.

Issue #1180 reported such a scenario which turned out to be related to the addition of an Interceptor to the OkHttpClient.

This change ensures the body is closed even if the response was unsuccessful.

Reviewed By: oprisnik

Differential Revision: D3311688

fbshipit-source-id: 6ac8046be199ba27a3bb3b9c4f932fd684801f41
  • Loading branch information
kirwan authored and Facebook Github Bot 4 committed May 24, 2016
1 parent e0df705 commit a490e16
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Expand Up @@ -117,12 +117,16 @@ public void onCancellationRequested() {
@Override
public void onResponse(Response response) {
fetchState.responseTime = SystemClock.uptimeMillis();
if (!response.isSuccessful()) {
handleException(call, new IOException("Unexpected HTTP code " + response), callback);
return;
}
final ResponseBody body = response.body();
try {
if (!response.isSuccessful()) {
handleException(
call,
new IOException("Unexpected HTTP code " + response),
callback);
return;
}

long contentLength = body.contentLength();
if (contentLength < 0) {
contentLength = 0;
Expand Down
Expand Up @@ -105,12 +105,16 @@ public void onCancellationRequested() {
@Override
public void onResponse(Call call, Response response) throws IOException {
fetchState.responseTime = SystemClock.elapsedRealtime();
if (!response.isSuccessful()) {
handleException(call, new IOException("Unexpected HTTP code " + response), callback);
return;
}
final ResponseBody body = response.body();
try {
if (!response.isSuccessful()) {
handleException(
call,
new IOException("Unexpected HTTP code " + response),
callback);
return;
}

long contentLength = body.contentLength();
if (contentLength < 0) {
contentLength = 0;
Expand Down

0 comments on commit a490e16

Please sign in to comment.