Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise a custom error if the RDB is an error report #15

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions dataretrieval/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class DataRetrievalError(BaseException):
"""Base exception for dataretrieval"""


class EmptyQueryResultError(DataRetrievalError):
"""Raised when a query returns an error"""
5 changes: 5 additions & 0 deletions dataretrieval/nwis.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from io import StringIO

from dataretrieval.utils import to_str, format_datetime, update_merge, set_metadata as set_md
from .exceptions import EmptyQueryResultError
from .utils import query

WATERDATA_BASE_URL = 'https://nwis.waterdata.usgs.gov/'
Expand Down Expand Up @@ -709,6 +710,10 @@ def _read_rdb(rdb):
fields = rdb.splitlines()[count].split('\t')
dtypes = {'site_no': str, 'dec_long_va': float, 'dec_lat_va': float}

# raise error if result has error
if "Error report" in (fields[0]):
raise EmptyQueryResultError

df = pd.read_csv(StringIO(rdb), delimiter='\t', skiprows=count + 2,
names=fields, na_values='NaN', dtype=dtypes)

Expand Down
9 changes: 8 additions & 1 deletion tests/nwis_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest
from dataretrieval.nwis import get_record

from dataretrieval.exceptions import EmptyQueryResultError
from dataretrieval.nwis import get_record, _read_rdb

START_DATE = '2018-01-24'
END_DATE = '2018-01-25'
Expand Down Expand Up @@ -39,6 +41,11 @@ def test_iv_service_answer():
assert df.index.names == [SITENO_COL, DATETIME_COL], "iv service returned incorrect index: {}".format(df.index.names)


def test_read_rdb_raises_error():
with pytest.raises(EmptyQueryResultError):
_read_rdb("Error report")


if __name__=='__main__':
test_measurements_service_answer()
test_iv_service_answer()