Skip to content

Commit cbf7075

Browse files
committed
Implement Copilot improvements: preserve tracebacks, robust HTML detection, and test refinement
1 parent 5cf5366 commit cbf7075

2 files changed

Lines changed: 39 additions & 21 deletions

File tree

dataretrieval/nwis.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -484,12 +484,16 @@ def get_dv(
484484
try:
485485
df = _read_json(response.json())
486486
except Exception as e:
487-
if "<html>" in response.text.lower():
487+
if (
488+
"<html>" in response.text.lower()
489+
or "<!doctype" in response.text.lower()
490+
or "text/html" in response.headers.get("Content-Type", "").lower()
491+
):
488492
raise ValueError(
489493
"Received HTML response instead of JSON. This often indicates "
490494
"that the service is currently unavailable."
491495
) from e
492-
raise e
496+
raise
493497

494498
return format_response(df, **kwargs), NWIS_Metadata(response, **kwargs)
495499

@@ -678,12 +682,16 @@ def get_iv(
678682
try:
679683
df = _read_json(response.json())
680684
except Exception as e:
681-
if "<html>" in response.text.lower():
685+
if (
686+
"<html>" in response.text.lower()
687+
or "<!doctype" in response.text.lower()
688+
or "text/html" in response.headers.get("Content-Type", "").lower()
689+
):
682690
raise ValueError(
683691
"Received HTML response instead of JSON. This often indicates "
684692
"that the service is currently unavailable."
685693
) from e
686-
raise e
694+
raise
687695
return format_response(df, **kwargs), NWIS_Metadata(response, **kwargs)
688696

689697

@@ -860,7 +868,7 @@ def get_record(
860868
- 'peaks': discharge peaks
861869
- 'gwlevels': (defunct) use `waterdata.get_field_measurements`
862870
- 'pmcodes': (defunct) use `get_reference_table`
863-
- 'water_use': (defunct) defunct
871+
- 'water_use': (defunct) no replacement available
864872
- 'ratings': get rating table
865873
- 'stat': get statistics
866874
ssl_check: bool, optional
@@ -886,8 +894,6 @@ def get_record(
886894
>>> # Get site description for site 01585200
887895
>>> df = dataretrieval.nwis.get_record(sites="01585200", service="site")
888896
889-
>>> # Get site description for site 01585200
890-
>>> df = dataretrieval.nwis.get_record(sites="01585200", service="site")
891897
892898
>>> # Get discharge peaks for site 01585200
893899
>>> df = dataretrieval.nwis.get_record(sites="01585200", service="peaks")
@@ -1232,7 +1238,11 @@ def site_info(self) -> tuple[pd.DataFrame, BaseMetadata] | None:
12321238
return None # don't set metadata site_info attribute
12331239

12341240
@property
1235-
def variable_info(self) -> tuple[pd.DataFrame, BaseMetadata] | None:
1241+
def variable_info(self) -> None:
1242+
"""
1243+
Deprecated. Accessing variable_info via NWIS_Metadata is deprecated
1244+
as it relied on the defunct `get_pmcodes` function. Returns None.
1245+
"""
12361246
# define variable_info metadata based on parameterCd if available
12371247
if "parameterCd" in self._parameters:
12381248
warnings.warn(

tests/nwis_test.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,35 @@ def test_iv_service_answer(requests_mock):
6161
DATETIME_COL,
6262
], f"iv service returned incorrect index: {df.index.names}"
6363

64-
65-
def test_nwis_service_live():
66-
"""Live sanity check of NWIS service, tolerant of 502/503."""
64+
"""Live sanity check of NWIS service, tolerant of transient NWIS outages."""
6765
site = "01491000"
6866
try:
6967
# Minimal query: just most recent record
7068
get_iv(sites=site)
7169
except ValueError as e:
72-
# Catch our custom 5xx error from utils.py
73-
if any(err in str(e) for err in ["502", "503", "Service Unavailable"]):
74-
pytest.skip(f"Service is currently unavailable (transient 502/503): {e}")
75-
raise e
70+
# Catch known transient service failures surfaced as ValueError
71+
error_text = str(e)
72+
if any(
73+
err in error_text
74+
for err in [
75+
"500",
76+
"502",
77+
"503",
78+
"Service Unavailable",
79+
"Received HTML response instead of JSON",
80+
]
81+
):
82+
pytest.skip(
83+
f"Service is currently unavailable (transient NWIS outage): {e}"
84+
)
85+
raise
7686
except Exception as e:
7787
# Fallback for other potential transient network issues
7888
if "Expecting value" in str(e) or "JSON" in str(e):
79-
pytest.skip(f"Service returned invalid response (likely 502/503): {e}")
80-
raise e
89+
pytest.skip(
90+
f"Service returned invalid response (likely transient outage): {e}"
91+
)
92+
raise
8193

8294

8395
def test_preformat_peaks_response():
@@ -93,10 +105,6 @@ def test_preformat_peaks_response():
93105
assert df["datetime"].isna().sum() == 0
94106

95107

96-
if __name__ == "__main__":
97-
test_iv_service_answer()
98-
99-
100108
# tests using real queries to USGS webservices
101109
# these specific queries represent some edge-cases and the tests to address
102110
# incomplete date-time information

0 commit comments

Comments
 (0)