From 229fcf39465f0913dcb9150849a871ff0d8a58ea Mon Sep 17 00:00:00 2001 From: Weimin Yu Date: Thu, 23 Jan 2025 11:27:03 -0500 Subject: [PATCH] UrlConnectionException loses error info (#2648) 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. --- .../google/registry/util/UrlConnectionException.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/util/src/main/java/google/registry/util/UrlConnectionException.java b/util/src/main/java/google/registry/util/UrlConnectionException.java index 019a50eb4..7e2cb6598 100644 --- a/util/src/main/java/google/registry/util/UrlConnectionException.java +++ b/util/src/main/java/google/registry/util/UrlConnectionException.java @@ -37,13 +37,14 @@ public class UrlConnectionException extends RuntimeException { @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)