Skip to content

Commit

Permalink
parser: don't expose parser-internal RetrieveError to callers. #307
Browse files Browse the repository at this point in the history
  • Loading branch information
lemon24 committed Sep 2, 2024
1 parent c51d8fb commit ffbd06f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/reader/_parser/_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ def parallel(
if isinstance(result.value, Exception):
if not isinstance(result.value, ParseError):
raise result.value

# don't expose parser-internal RetrieveError to callers
# TODO: not needed once RetrieveError is public API
if isinstance(result.value, RetrieveError):
e = result.value
value = ParseError(e.url, message=e.message)
value.__traceback__ = e.__traceback__
value.__cause__ = e.__cause__
result = result._replace(value=value)

yield cast(ParseResult[F, ParseError], result)

def __call__(
Expand Down
17 changes: 12 additions & 5 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,8 @@ def do_raise(*args, **kwargs):
assert excinfo.value.url == feed_url
assert 'while getting feed' in excinfo.value.message

assert getattr(excinfo.value, 'http_info', None) is None
assert not hasattr(excinfo.value, 'http_info')

assert parse.last_result.http_info is None


Expand All @@ -492,7 +493,7 @@ def do_raise(*args, **kwargs):
assert excinfo.value.url == feed_url
assert 'while reading feed' in excinfo.value.message

assert excinfo.value.http_info is parse.last_result.http_info
assert not hasattr(excinfo.value, 'http_info')

info = parse.last_result.http_info
assert info.status == 200
Expand Down Expand Up @@ -1132,19 +1133,25 @@ def retrieve(*_, **__):

def test_retrieved_feed_http_info_not_shadowed_by_retrieve_error(parse):
exc = RetrieveError('x', http_info=HTTPInfo(333, {}))
cause = ValueError('whatever')

class file:
def read(*_):
raise exc
raise exc from cause

@contextmanager
def retrieve(*_, **__):
yield RetrievedFeed(file, http_info=HTTPInfo(200, {}), slow_to_read=True)

parse.retrieve = retrieve

with pytest.raises(RetrieveError) as exc_info:
with pytest.raises(ParseError) as exc_info:
parse('http://example.com')
assert exc_info.value is exc

assert type(exc_info.value) is ParseError
assert exc_info.value.url is exc.url
assert exc_info.value.message is exc.message
# not sure how to check the traceback was preserved
assert exc_info.value.__cause__ is cause

assert parse.last_result.http_info == HTTPInfo(200, {})

0 comments on commit ffbd06f

Please sign in to comment.