Python
Bedrock verify: detect ClientError by response shape, not class name
Devika Vermad781cb11c137
diff --git a/coworker/providers/registry.py b/coworker/providers/registry.pyindex 6c147705..8f2b094b 100644--- a/coworker/providers/registry.py+++ b/coworker/providers/registry.py@@ -688,16 +688,27 @@ def _verify_bedrock(fields: dict[str, Any], timeout: float) -> dict[str, Any]:}if kind == "ProfileNotFound":return {"ok": False, "error": f"{exc}"}- if kind == "ClientError":- code = (getattr(exc, "response", {}) or {}).get("Error", {}).get("Code", "")+ # botocore raises MODELED subclasses of ClientError (class name+ # "AccessDeniedException", not "ClientError"), so detect by the response shape,+ # never the class name — a name check sent every modeled error to the generic+ # fallback and hid the specific message (owner report 2026-08-17).+ code = (getattr(exc, "response", None) or {}).get("Error", {}).get("Code", "")+ if code:if code in ("UnrecognizedClientException", "InvalidSignatureException"):return {"ok": False, "error": "AWS rejected the credentials."}if code in ("AccessDeniedException", "AccessDenied"):return {"ok": False,- "error": "Credentials work but lack Bedrock access (bedrock:ListFoundationModels).",+ "error": (+ "Credentials work but lack Bedrock access "+ "(bedrock:ListFoundationModels) — check the key's policy, its "+ "expiry (short-term keys last up to 12h), and that the region "+ "matches where the key was created."+ ),}- return {"ok": False, "error": f"AWS Bedrock returned {code or kind}."}+ 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": True}diff --git a/tests/test_bedrock_provider.py b/tests/test_bedrock_provider.pyindex 97ed179e..029ebc74 100644--- a/tests/test_bedrock_provider.py+++ b/tests/test_bedrock_provider.py@@ -576,3 +576,34 @@ def test_verify_bedrock_maps_client_errors(monkeypatch):"bedrock", fields={"region": "us-east-1", "auth_method": "profile"})assert not out["ok"] and "rejected" in out["error"]+++def test_verify_bedrock_maps_modeled_client_error_subclasses(monkeypatch):+ # Live boto3 raises MODELED subclasses (class name "AccessDeniedException", not+ # "ClientError"). The old name-based check sent these to the generic "Couldn't reach"+ # fallback and hid the specific guidance (owner report 2026-08-17, real Bedrock key).+ from botocore.exceptions import ClientError++ from coworker.providers.registry import verify_provider_key++ modeled_cls = type("AccessDeniedException", (ClientError,), {})+ denied = modeled_cls(+ {"Error": {"Code": "AccessDeniedException", "Message": "no"}},+ "ListFoundationModels",+ )+ _patch_session(monkeypatch, _FakeBedrockControl(exc=denied), {})+ out = verify_provider_key(+ "bedrock", fields={"region": "us-east-1", "auth_method": "profile"}+ )+ assert not out["ok"] and "Bedrock access" in out["error"]+ assert "Couldn't reach" not in out["error"]++ expired = type("ExpiredTokenException", (ClientError,), {})(+ {"Error": {"Code": "ExpiredTokenException", "Message": "no"}},+ "ListFoundationModels",+ )+ _patch_session(monkeypatch, _FakeBedrockControl(exc=expired), {})+ out = verify_provider_key(+ "bedrock", fields={"region": "us-east-1", "auth_method": "profile"}+ )+ assert not out["ok"] and "expired" in out["error"]