Repository: louislam/uptime-kuma
File: server/monitor-types/rabbitmq.js (JavaScript)

Code before:
        }

        const errors = [];

        for (let i = 0; i < baseUrls.length; i++) {
            const baseUrl = baseUrls[i];
            const nodeIndex = i + 1;
            const totalNodes = baseUrls.length;

            try {
                await this.checkSingleNode(monitor, heartbeat, baseUrl, nodeIndex, totalNodes);
                // If checkSingleNode succeeds, heartbeat is set to UP and we can return
                return;
            } catch (error) {
                const errorMsg = `Node ${nodeIndex}: ${error.message}`;
                log.warn("monitor", `[${monitor.name}] ${errorMsg}`);
                errors.push(errorMsg);
            }
        }

        // If we reach here, all nodes failed
        const consolidatedError = errors.length > 1 
            ? `All ${errors.length} nodes failed: ${errors.join("; ")}`
            : errors[0] || "All RabbitMQ nodes are unavailable";
        log.error("monitor", `[${monitor.name}] ${consolidatedError}`);
        throw new Error(consolidatedError);
    }

    /**
     * Check a single RabbitMQ node
     * @param {object} monitor Monitor configuration
     * @param {object} heartbeat Heartbeat object to update
     * @param {string} baseUrl Base URL of the RabbitMQ node
     * @param {number} nodeIndex Index of the current node (1-based for logging)
     * @param {number} totalNodes Total number of nodes configured
     * @returns {Promise<void>}
     * @throws {Error} If the node check fails
     */
    async checkSingleNode(monitor, heartbeat, baseUrl, nodeIndex, totalNodes) {
        // Without a trailing slash, path in baseUrl will be removed. https://example.com/api -> https://example.com
        let normalizedUrl = baseUrl;
        if (!normalizedUrl.endsWith("/")) {
            normalizedUrl += "/";
        }

        const options = {
            // Do not start with slash, it will strip the trailing slash from baseUrl
            url: new URL("api/health/checks/alarms/", normalizedUrl).href,
            method: "get",
            timeout: monitor.timeout * 1000,
            headers: {

Review comment (bug):
there must be more than 1 error at this point.
Also, this is already logged outside this function

```suggestion
        // If we reach here, all nodes failed.
        throw new Error(`All ${errors.length} nodes failed because ${errors.join("; ")}`);
```

Code after:
        }

        const errors = [];

        for (let i = 0; i < baseUrls.length; i++) {
            const baseUrl = baseUrls[i];
            const nodeIndex = i + 1;

            try {
                await this.checkSingleNode(monitor, baseUrl, `${nodeIndex}/${baseUrls.length}`);
                // If checkSingleNode succeeds (doesn't throw), set heartbeat to UP
                heartbeat.status = UP;
                heartbeat.msg = baseUrls.length === 1 ? "Node is reachable and there are no alerts in the cluster" : `One of the ${baseUrls.length} nodes is reachable and there are no alerts in the cluster`;
                return;
            } catch (error) {
                log.warn(this.name, `Node ${nodeIndex}: ${error.message}`);
                errors.push(`Node ${nodeIndex}: ${error.message}`);
            }
        }

        // If we reach here, all nodes failed
        throw new Error(`All ${errors.length} nodes failed because ${errors.join("; ")}`);
    }

    /**
     * Check a single RabbitMQ node
     * @param {object} monitor Monitor configuration
     * @param {string} baseUrl Base URL of the RabbitMQ node
     * @param {string} nodeInfo Node index info for logging (e.g., "1/3")
     * @returns {Promise<void>}
     * @throws {Error} If the node check fails
     */
    async checkSingleNode(monitor, baseUrl, nodeInfo) {
        // Without a trailing slash, path in baseUrl will be removed. https://example.com/api -> https://example.com
        let normalizedUrl = baseUrl;
        if (!normalizedUrl.endsWith("/")) {
            normalizedUrl += "/";
        }

        const options = {
            // Do not start with slash, it will strip the trailing slash from baseUrl
            url: new URL("api/health/checks/alarms/", normalizedUrl).href,
            method: "get",
            timeout: monitor.timeout * 1000,
            headers: {
                "Accept": "application/json",
                "Authorization": "Basic " + Buffer.from(`${monitor.rabbitmqUsername || ""}:${monitor.rabbitmqPassword || ""}`).toString("base64"),
            },
            signal: axiosAbortSignal((monitor.timeout + 10) * 1000),
            // Capture reason for 503 status
            validateStatus: (status) => status === 200 || status === 503,