-
Notifications
You must be signed in to change notification settings - Fork 662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Response body empty when HttpException throw #365
Response body empty when HttpException throw #365
Conversation
} catch (HttpException httpException) { | ||
assertThat(httpException.code()).isEqualTo(401); | ||
assertThat(httpException.message()).isEqualTo("Client Error"); | ||
assertThat(httpException.rawResponse().body().string()).isEqualTo("Unauthorized request!"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is is a good idea to not close the body of the response? I am not sure of the implications of not doing so. Personally, I would have changed the HttpException
class to have a static method that looks like:
public static HttpException from(Response response) throws IOException {
try {
String body = response.body() != null ? response.body().string() : "";
return new HttpException(response, response.code(), response.message(), body);
} finally {
closeQuietly(response);
}
}
So that the body does get closed but before it does, we have copied over the contents of it. Also IIRC, the ResponseBody
in the Response
object is what gets closed when close
is called. So if we read and copy the contents over, it is probably safe to close.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to add, calling string()
does close ResponseBody
but if no one was to call it, it would not close.
private final okhttp3.Response rawResponse; | ||
private final int code; | ||
private final String message; | ||
private final Response rawResponse; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically Exceptions
should be Serializable
. Since Response
isn't Serializable
, this is a good time to add transient
to its definition
Just throwing out an idea here. What if we reserve I mention this because as of right now, I think the public api for the
Thoughts on this? I realize it might require a bigger, breaking, change but may be worth it. |
@mandrizzle Thanks, these are good points:
Except IOException we can potentially have:
That the tradeoff of giving user to parse error body. And this is the contract that user must follow always close
That's issue of Rx wrapper if user don't want to break the rx chain he must wrap the error (for example with onErrorResumeNext or smth). So this is not part of ApolloClient itself as it's a pure java module, but we can think about this in context of our RxWrapper |
Closes #361
@mandrizzle