public static String buildHttpErrorMessage(final HttpURLConnection connection) throws IOException {
        val messageBuilder = new StringBuilder("(").append(connection.getResponseCode()).append(")");
        if (connection.getResponseMessage() != null) {
            messageBuilder.append(" ");
            messageBuilder.append(connection.getResponseMessage());
        }
        try (var isr = new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8);
             var br = new BufferedReader(isr)) {
            String output;
            messageBuilder.append("[");
            while ((output = br.readLine()) != null) {
                messageBuilder.append(output);
            }
            messageBuilder.append("]");
        } finally {
            connection.disconnect();
        }
        return messageBuilder.toString();
    }
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
pac4j-core/src/main/java/org/pac4j/core/util/HttpUtils.java