-
Notifications
You must be signed in to change notification settings - Fork 974
Check Response Code before using conn.getInputStream #144
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
Conversation
Current coverage is 79.23% (diff: 100%)@@ master #144 diff @@
==========================================
Files 41 41
Lines 2121 2124 +3
Methods 0 0
Messages 0 0
Branches 258 260 +2
==========================================
- Hits 1744 1683 -61
+ Misses 377 362 -15
- Partials 0 79 +79
|
If the response code is a non 200, use conn.getErrorStream instead. This will allow things like token refresh errors to be handled correctly. Currently, you get an AuthorizationException with a FileNotFoundException as it's cause due to the call to conn.getInputStream.
I'd prefer the solution to read the conn.getInputStream() and if it throws read the conn.getErrorStream() What is the benefit of your status-code checking approach? |
Not a fan of using exceptions as a means of flow control |
Me neither, since I used to develop for iOS. But Java itself IS an exceptional programming language. |
Plus, performance wise, it's much faster to do a response code check than to wait for an exception to be thrown. Without getting into all the details, you essentially have to create and unwind new stack elements to do it. Avoiding the exception in the first place, IMHO, is usually the best approach. |
I guess we have a very different opinion in this topic. I really neither like nor appreciate this exception stuff, but the whole language is based on this. If you prefer to completely rely on status checks then another pull request may fit your needs. |
To me, this is not disruption to the common flow. The fact is that the spec says if you have an error response (ie, a 400 Bad Request), then you should use getErrorStream instead. This is EXACTLY what my code implements. For example, which of these code blocks makes more sense to you:
To me it's the first. |
In any case, whether it's my PR or your PR is really up to the project maintainers. But it's blocking my development :( hopefully @iainmcgin can get to this soon. |
Guess that's the main issue. To me it is the latter. (in Java) |
Apologies, I've been out for a few weeks. I'll evaluate the PRs and include a fix for this for 0.5.0. |
Review status: 0 of 2 files reviewed at latest revision, all discussions resolved. Comments from Reviewable |
Generally speaking, I agree with @mattinger - the API of HttpURLConnection provides a way to predict whether getInputStream() would throw an exception or not, so it is better to use this mechanism (check the response code). If @mfeltmann's counter-argument were correct, then perhaps one would think the following code is acceptable: Iterator<String> strIter = strings.iterator();
try {
while(true) {
System.out.println(strIter.next());
}
} catch (NoSuchElementException ex) {
System.out.println("END OF LIST");
} Rather than using hasNext() as the loop condition. |
If the response code is a non 200, use conn.getErrorStream instead.
This will allow things like token refresh errors to be handled correctly.
Currently, you get an AuthorizationException with a FileNotFoundException
as it's cause due to the call to conn.getInputStream.
This change is