Python
verify: surface the transport error's cause, not just its class name
Mr-Neutr0n16132bcc75c1
diff --git a/coworker/providers/registry.py b/coworker/providers/registry.pyindex 397efee1..73b9e4b8 100644--- a/coworker/providers/registry.py+++ b/coworker/providers/registry.py@@ -830,7 +830,10 @@ def _verify_bedrock(fields: dict[str, Any], timeout: float) -> dict[str, Any]:if code == "ExpiredTokenException":return {"ok": False, "error": "The credentials have expired — generate a new key."}return {"ok": False, "error": f"AWS Bedrock returned {code}."}- return {"ok": False, "error": f"Couldn't reach AWS Bedrock ({kind})."}+ return {+ "ok": False,+ "error": f"Couldn't reach AWS Bedrock ({_describe_transport_error(exc)})."+ }return {"ok": True}@@ -869,7 +872,7 @@ def _verify_vertex(fields: dict[str, Any], timeout: float) -> dict[str, Any]:except Exception as exc:return {"ok": False,- "error": f"Couldn't reach Vertex AI ({exc.__class__.__name__}).",+ "error": f"Couldn't reach Vertex AI ({_describe_transport_error(exc)}).",}if resp.status_code < 300:return {"ok": True}@@ -914,7 +917,10 @@ def _verify_vertex(fields: dict[str, Any], timeout: float) -> dict[str, Any]:timeout=timeout,)except Exception as exc:- return {"ok": False, "error": f"Couldn't reach Vertex AI ({exc.__class__.__name__})."}+ return {+ "ok": False,+ "error": f"Couldn't reach Vertex AI ({_describe_transport_error(exc)}).",+ }if resp.status_code < 300:return {"ok": True}if resp.status_code in (401, 403):@@ -927,6 +933,37 @@ def _verify_vertex(fields: dict[str, Any], timeout: float) -> dict[str, Any]:return {"ok": False, "error": f"Vertex AI returned HTTP {resp.status_code}."}+def _describe_transport_error(exc: Exception) -> str:+ """Turn a verify probe's transport failure into one actionable line.++ The bare class name ("ConnectError") cost a real user round-trips of+ diagnosis while the exception carried the answer all along — TLS+ verification, DNS, refusal and timeout each read differently (#505).+ """+ msg = " ".join(str(exc).split()).rstrip(".")[:200]+ lowered = msg.lower()+ for signature, hint in (+ (+ "certificate_verify_failed",+ "TLS certificate verification failed; the app's CA bundle may be missing or stale",+ ),+ ("ssl:", "TLS handshake failed"),+ ("gaierror", "DNS could not resolve the endpoint"),+ ("nodename", "DNS could not resolve the endpoint"),+ ("name or service", "DNS could not resolve the endpoint"),+ ("getaddrinfo", "DNS could not resolve the endpoint"),+ ("refused", "the endpoint refused the connection"),+ ("timed out", "the connection timed out"),+ ):+ if signature in lowered:+ if not msg:+ return f"{exc.__class__.__name__} — {hint}"+ return f"{exc.__class__.__name__}: {msg} — looks like {hint}"+ if not msg:+ return exc.__class__.__name__+ return f"{exc.__class__.__name__}: {msg}"++def verify_provider_key(name: str,*,@@ -1003,7 +1040,7 @@ def verify_provider_key(except Exception as exc: # DNS/connection/timeout — never let it bubble to a 500return {"ok": False,- "error": f"Couldn't reach {d.title} ({exc.__class__.__name__}).",+ "error": f"Couldn't reach {d.title} ({_describe_transport_error(exc)}).",}if resp.status_code < 300:diff --git a/tests/test_provider_verify.py b/tests/test_provider_verify.pyindex a6e553af..bb7d338b 100644--- a/tests/test_provider_verify.py+++ b/tests/test_provider_verify.py@@ -161,3 +161,45 @@ def test_verify_unexpected_status(monkeypatch):res = verify_provider_key("anthropic", api_key="sk-ant-x")assert res["ok"] is Falseassert "500" in res["error"]+++# -- verify_provider_key: transport failures carry their cause (#505) ------------+def test_verify_transport_failure_includes_cause_and_hint(monkeypatch):+ import httpx++ _patch_get(+ monkeypatch,+ raise_exc=httpx.ConnectError(+ "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: "+ "unable to get local issuer certificate (_ssl.c:1000)"+ ),+ )+ result = verify_provider_key("openai", api_key="sk-x")+ assert result["ok"] is False+ assert "CERTIFICATE_VERIFY_FAILED" in result["error"]+ assert "CA bundle" in result["error"]+++def test_verify_dns_failure_names_dns(monkeypatch):+ import httpx++ _patch_get(monkeypatch, raise_exc=httpx.ConnectError("[Errno -3] nodename nor servname provided"))+ error = verify_provider_key("openai", api_key="sk-x")["error"]+ assert "DNS" in error+ assert "[Errno -3]" in error+++def test_verify_timeout_says_timed_out(monkeypatch):+ import httpx++ _patch_get(monkeypatch, raise_exc=httpx.ConnectTimeout("The request timed out."))+ error = verify_provider_key("openai", api_key="sk-x")["error"]+ assert "timed out" in error+++def test_verify_silent_exception_falls_back_to_class_name_only(monkeypatch):+ class Weird(Exception):+ pass++ _patch_get(monkeypatch, raise_exc=Weird())+ assert verify_provider_key("openai", api_key="sk-x") == {"ok": False, "error": "Couldn't reach OpenAI (Weird)."}