From 8728aef0b53677d1198e0e3e65207710a02aa9dc Mon Sep 17 00:00:00 2001 From: Arjun Satarkar Date: Fri, 4 Aug 2023 04:20:42 +0530 Subject: Improve handling of network errors --- serve.py | 11 ++++++++--- tagrss.py | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/serve.py b/serve.py index eaa8bda..d04509f 100755 --- a/serve.py +++ b/serve.py @@ -186,13 +186,18 @@ def add_feed_effect(): "title.", ) except tagrss.FeedFetchError as e: - try: - if e.status_code != 200: + if e.bad_source: + if getattr(e, "status_code", None): raise bottle.HTTPError( 400, f'Could not fetch feed: "{feed_source}" returned HTTP status code {e.status_code}.', ) - except AttributeError: + else: + raise bottle.HTTPError( + 400, + f'Could not fetch feed from "{feed_source}" due to a problem with the source.', + ) + else: raise bottle.HTTPError(500, f"Failed to fetch feed from {feed_source}.") return bottle.template( "add_feed", diff --git a/tagrss.py b/tagrss.py index ce67047..4370b7a 100644 --- a/tagrss.py +++ b/tagrss.py @@ -33,18 +33,28 @@ class FeedTitleAlreadyInUseError(StorageError): class StorageConstraintViolationError(StorageError): pass + class SqliteMissingForeignKeySupportError(StorageError): pass class FeedFetchError(Exception): - def __init__(self, *, feed_source: str, status_code: typing.Optional[int] = None, underlying: typing.Optional[Exception] = None): + def __init__( + self, + *, + feed_source: str, + bad_source: bool = False, + status_code: typing.Optional[int] = None, + underlying: typing.Optional[Exception] = None, + ): + self.bad_source = bad_source if status_code: self.status_code = status_code super().__init__(f"Get {feed_source} returned HTTP {status_code}") else: super().__init__(f"Get {feed_source} failed: {underlying}") + FeedId = int Epoch = int ParsedFeed = feedparser.FeedParserDict @@ -398,9 +408,13 @@ class TagRss: response = requests.get(source) except requests.ConnectionError as e: raise FeedFetchError(feed_source=source, underlying=e) + except requests.exceptions.MissingSchema as e: + raise FeedFetchError(feed_source=source, bad_source=True, underlying=e) epoch_downloaded: int = int(time.time()) if response.status_code != requests.codes.ok: - raise FeedFetchError(feed_source=source, status_code=response.status_code) + raise FeedFetchError( + feed_source=source, bad_source=True, status_code=response.status_code + ) try: base: str = response.headers["Content-Location"] except KeyError: -- cgit v1.2.3-57-g22cb