Skip to content

Commit 5b8a661

Browse files
thodson-usgsclaude
andcommitted
Standardize exception type and document return for query()
Per copilot review on PR #253: - Re-raise the catch-all 4xx/5xx as ValueError with status/reason/url for parity with the explicit 400/404/414/500/502/503 branches; callers no longer need to handle both ValueError and HTTPError from the same helper. - Update the Returns section to document the actual return type (requests.Response) and the ValueError raise contract. - Narrow the test from pytest.raises(Exception) to pytest.raises( ValueError) with a match on the status code to assert the contract. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent c71b865 commit 5b8a661

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

dataretrieval/utils.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,17 @@ def query(url, payload, delimiter=",", ssl_check=True):
172172
173173
Returns
174174
-------
175-
string: query response
176-
The response from the API query ``requests.get`` function call.
175+
response : ``requests.Response``
176+
The response object from the underlying ``requests.get`` call.
177+
178+
Raises
179+
------
180+
ValueError
181+
For any non-success HTTP status (4xx/5xx). The message includes
182+
the status code, reason, and URL. Specific guidance is included
183+
for 400, 404, 414, and 500/502/503 statuses; any other 4xx/5xx
184+
falls through to a generic ValueError so callers don't silently
185+
receive an HTML error page as if it were data.
177186
"""
178187
params = {key: to_str(value, delimiter) for key, value in payload.items()}
179188

@@ -214,7 +223,12 @@ def query(url, payload, delimiter=",", ssl_check=True):
214223

215224
# Catch-all for any other 4xx/5xx (401, 403, 405, 408, 429, 501, 504, ...)
216225
# so callers don't silently receive an HTML error page as if it were data.
217-
response.raise_for_status()
226+
# Re-raise as ValueError to keep the exception contract uniform with the
227+
# explicit status branches above.
228+
if not response.ok:
229+
raise ValueError(
230+
f"HTTP {response.status_code} {response.reason} for {response.url}"
231+
)
218232

219233
if response.text.startswith("No sites/data"):
220234
raise NoSitesError(response.url)

tests/utils_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def test_unhandled_4xx_5xx_raises(self, requests_mock, status):
6969
"""
7070
url = "https://example.com/svc"
7171
requests_mock.get(url, status_code=status, text="<html>denied</html>")
72-
with pytest.raises(Exception): # noqa: B017 -- HTTPError or ValueError
72+
with pytest.raises(ValueError, match=str(status)):
7373
utils.query(url, {"k": "v"})
7474

7575

0 commit comments

Comments
 (0)