From 47cb9f661fae4346426fd2ad9cc5c55359eaade3 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Tue, 5 May 2026 20:18:50 -0700 Subject: [PATCH] fix: narrow exception handling in list_nodes, move mock imports to top - Remove redundant httpx.ConnectError from nodes except clause (it's a subclass of HTTPError so the tuple catch was redundant) - Narrow services except clause from bare Exception to httpx.HTTPError, add logger.warning with coordinator_url for debuggability - Move `from unittest.mock import MagicMock, patch` from mid-file to the top-of-file import block with the other stdlib/third-party imports --- app/nodes.py | 5 +++-- tests/test_nodes.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/nodes.py b/app/nodes.py index cf29d40..891f56d 100644 --- a/app/nodes.py +++ b/app/nodes.py @@ -117,7 +117,7 @@ def list_nodes() -> list: r = httpx.get(f"{coordinator_url}/api/nodes", timeout=5.0) r.raise_for_status() coord_nodes: list[dict] = r.json() - except (httpx.HTTPError, httpx.ConnectError) as exc: + except httpx.HTTPError as exc: logger.warning("Coordinator unreachable: %s", exc) return [] @@ -125,7 +125,8 @@ def list_nodes() -> list: sr = httpx.get(f"{coordinator_url}/api/services", timeout=5.0) sr.raise_for_status() services_data: list[dict] = sr.json() - except Exception: + except httpx.HTTPError: + logger.warning("Services API unreachable for %s, skipping", coordinator_url) services_data = [] # Build per-node, per-GPU running services map diff --git a/tests/test_nodes.py b/tests/test_nodes.py index 2b6e74a..52df098 100644 --- a/tests/test_nodes.py +++ b/tests/test_nodes.py @@ -6,6 +6,7 @@ from pathlib import Path import pytest import yaml from fastapi.testclient import TestClient +from unittest.mock import MagicMock, patch @pytest.fixture(autouse=True) @@ -47,7 +48,6 @@ def test_list_nodes_returns_empty_when_no_coordinator(client): assert r.json() == [] -from unittest.mock import MagicMock, patch def _fake_nodes_response(nodes_json: list, services_json: list | None = None):