code: public static String buildHttpErrorMessage(final HttpURLConnection connection) throws IOException {
          final StringBuilder messageBuilder = new StringBuilder("(").append(connection.getResponseCode()).append(")");
          if (connection.getResponseMessage() != null) {
              messageBuilder.append(" ");
              messageBuilder.append(connection.getResponseMessage());
          }
          try (final InputStreamReader isr = new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8);
               BufferedReader br = new BufferedReader(isr)){
              String output;
              messageBuilder.append("[");
              while ((output = br.readLine()) != null) {
                  messageBuilder.append(output);
              }
              messageBuilder.append("]");
          }finally {
              connection.disconnect();
          }
          return messageBuilder.toString();
      }
docs: Build error message from connection in case of failure
  @param connection HttpURLConnection
  @return String by combining response code, message and error stream
  @throws IOException an IO exception