Skip to content

Commit

Permalink
UrlConnectionException loses error info (#2648)
Browse files Browse the repository at this point in the history
It does not get the error message for 400+ status codes.

It fails to get the status code if the response has neither data nor
error.
  • Loading branch information
weiminyu authored Jan 23, 2025
1 parent b775e4a commit 229fcf3
Showing 1 changed file with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ public UrlConnectionException(String message, HttpURLConnection connection) {
@Override
public String getMessage() {
byte[] resultContent;
int responseCode;
int responseCode = 0;
try {
resultContent = ByteStreams.toByteArray(connection.getInputStream());
responseCode = connection.getResponseCode();
} catch (IOException e) {
resultContent = new byte[] {};
responseCode = 0;
resultContent =
ByteStreams.toByteArray(
responseCode < 400 ? connection.getInputStream() : connection.getErrorStream());
} catch (IOException | NullPointerException e) {
resultContent = "-- Response is missing or has malformed content --".getBytes(UTF_8);
}
StringBuilder result =
new StringBuilder(2048 + resultContent.length)
Expand Down

0 comments on commit 229fcf3

Please sign in to comment.