-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Add error response field to InvalidResponseCodeException #6853
Comments
#3920 was requesting this as well. @chrisfillmore - Is there a reason to use 403 rather than 200 for this case (see the issue ref'd above for more information)? |
In our particular case we get HTTP 400 with an error code in the response body. It's not feasible for us to change the HTTP status from the license proxy; this would have a far-reaching impact on other client platforms we support. But I also don't think that 200 is an appropriate status for this response. The server wants to indicate the request is bad, hence 400. Is there a particular reason you'd prefer not to add this field? I'll admit, to me this seems a straightforward change with obvious benefit, without breaking anything. Am I missing something? |
Agreed it's not particularly difficult. There's a weird case in which there's an error reading the body though. What would you end up throwing in that case? To throw the |
@ojw28 Hi,
|
Thanks for the information. How did you handle the case mentioned above (i.e. failure whilst reading the response body)? |
In that case the responseBody on the will be empty, and when handling the error i will look only at the responseCode, and treat the error as a generic. |
Thinking about it, there is not a great solution to the problem of "what happens if reading the error response fails". I think the right thing to do, in that case, is to set the new error response field to an empty string or byte array. It's not perfect but it is an incremental improvement. If users run into this particular problem, then they could investigate a better solution. What do you think? |
It seems quite error prone to do that. If someone has a service where the empty body means something different to a non-empty body, they'll have no way to disambiguate whether an empty body is because the read failed, or because the body really was empty. So I think my preference would still be to throw "something else" in that case. Which is also consistent with what would happen if there was a problem reading the response headers, I think. |
@ojw28 i think that's a good idea. Throw "something else" when trying to read the responseBody from the InvalidResponseCodeException. |
Some ideas:
What do you guys have in mind for throwing "something else"? I think we would still want to throw InvalidResponseCodeException, since this exception still applies. |
@chrisfillmore i was thinking in the same lines of your point 3. The method getResponseBody() on the InvalidResponseCodeException would throw the exception originated from the parse of the error stream. |
@jrocharodrigues ok I see what you mean now. That's a good idea; I'm on board. @ojw28 do we have your blessing? |
My suggestion was actually to throw something simpler. I'd be tempted just to treat it as a generic
Note that if the body isn't always small in this case, then it's flawed to be trying to read it into the exception in the first place. |
Ok, I want to take a closer look at the behaviour of
But Android Studio tells me that getErrorStream() never returns null. To be safe, I'm going to add a null check. I'll open a draft PR so you can give me some feedback. |
Issue google#6853 Previously the body of an HTTP response was not included in an InvalidResponseCodeException. This change adds a field to store the response body, and updates DefaultHttpDataSource to populate this value.
This change supplies the response body in OkHttpDataSource to InvalidResponseCodeException. Issue google#6853
This is now implemented in |
[REQUIRED] Use case description
I would like to include the HTTP response body in an InvalidResponseCodeException, so that I can read the response.
In particular, our license server sends back specific error codes which we need to triage.
Proposed solution
Add a new field to InvalidResponseCodeException which stores the value returned from
HttpURLConnection#getErrorStream()
(or string equivalent).Alternatives considered
We could make our license request without using Exo's APIs. But I think making the change in Exo would be useful for everyone, and backwards-compatible.
I can make a pull request, with your support.
The text was updated successfully, but these errors were encountered: