You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We could throw an IllegalStateException instead. Applications shouldn't ever attempt to close the response returned from a cacheResponse() or networkResponse().
Response.close() correctly only deals with body (ResponseBody) and does not touch the others.
Finally maybe I'm wrong but in the end just adding a body != null check sound like the proper handling as there's no real error to manage in fact, a Closeable should not NPE on close.
It's an error to attempt to close a response that doesn't have a body.
@swankjesse how strongly do you hold this opinion? If this were not an error condition, then users could use Response in a try-with-resources statement in scenarios where the body is not considered. Take this example which checks for a successful status code and ignores the body
Responseresponse = null;
try {
response = client.newCall(request).execute();
if (!response.isSuccessful()) {
thrownewIllegalStateException("HTTP server returned error code " + response.code());
}
} finally {
if (response != null && response.body() != null) {
response.close();
}
}
If this code didn't need to first check response.body != null to avoid an error condition, then we could get rid of boilerplate with a try-with-resources statement.
try(Responseresponse = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
thrownewIllegalStateException("HTTP server returned error code " + response.code());
}
}
Your example above is safe. You have to try hard to get a response without a body – it’s the response used by Response.networkResponse() or Response.cacheResponse().
but I can fix this by adding an empty body to the builder chain and remain confident that for my use case responses should always have a non-null body.
Activity
swankjesse commentedon Jun 2, 2017
We could throw an IllegalStateException instead. Applications shouldn't ever attempt to close the response returned from a
cacheResponse()ornetworkResponse().Tolriq commentedon Jun 2, 2017
Not sure to understand
From code https://github.com/square/okhttp/blob/c70e35802e984e8b9bf3fbf1479deb37c0bdfa43/okhttp/src/main/java/okhttp3/Response.java#L274
Response.close() correctly only deals with body (ResponseBody) and does not touch the others.
Finally maybe I'm wrong but in the end just adding a body != null check sound like the proper handling as there's no real error to manage in fact, a Closeable should not NPE on close.
swankjesse commentedon Jun 2, 2017
It's an error to attempt to close a response that doesn't have a body.
Clarify documentation on closing WebSockets and Responses.
gilday commentedon Nov 14, 2018
@swankjesse how strongly do you hold this opinion? If this were not an error condition, then users could use
Responsein a try-with-resources statement in scenarios where the body is not considered. Take this example which checks for a successful status code and ignores the bodyIf this code didn't need to first check
response.body != nullto avoid an error condition, then we could get rid of boilerplate with a try-with-resources statement.swankjesse commentedon Nov 15, 2018
Your example above is safe. You have to try hard to get a response without a body – it’s the response used by
Response.networkResponse()orResponse.cacheResponse().gilday commentedon Nov 15, 2018
Cool, that helps. I was running into trouble in a unit test where I was building a
Responsemyself and I omitted the body e.g.but I can fix this by adding an empty body to the builder chain and remain confident that for my use case responses should always have a non-null body.
thanks!