Skip to content

Commit 38f24eb

Browse files
authored
fix: handle errors in the body of the response (#18023)
1 parent 7c9f199 commit 38f24eb

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

tests/unit/accounts/test_services.py

+28
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,34 @@ def __init__(self):
17001700
)
17011701
]
17021702

1703+
def test_domainr_response_contains_errors_returns_none(self):
1704+
response = pretend.stub(
1705+
json=lambda: {
1706+
"status": [],
1707+
"errors": [
1708+
{
1709+
"code": 400,
1710+
"detail": "unknown zone: ocm",
1711+
"message": "Bad request",
1712+
}
1713+
],
1714+
},
1715+
raise_for_status=lambda: None,
1716+
)
1717+
session = pretend.stub(get=pretend.call_recorder(lambda *a, **kw: response))
1718+
svc = services.DomainrDomainStatusService(
1719+
session=session, client_id="some_client_id"
1720+
)
1721+
1722+
assert svc.get_domain_status("example.ocm") is None
1723+
assert session.get.calls == [
1724+
pretend.call(
1725+
"https://api.domainr.com/v2/status",
1726+
params={"client_id": "some_client_id", "domain": "example.ocm"},
1727+
timeout=5,
1728+
)
1729+
]
1730+
17031731
def test_factory(self):
17041732
context = pretend.stub()
17051733
request = pretend.stub(

warehouse/accounts/services.py

+4
Original file line numberDiff line numberDiff line change
@@ -1008,4 +1008,8 @@ def get_domain_status(self, domain: str) -> list[str] | None:
10081008
logger.warning("Error contacting Domainr: %r", exc)
10091009
return None
10101010

1011+
if errors := resp.json().get("errors"):
1012+
logger.warning("Error from Domainr: %r", errors)
1013+
return None
1014+
10111015
return resp.json()["status"][0]["status"].split()

0 commit comments

Comments
 (0)