From 353001c9258de2cd1f8fcc026ea954cae624f669 Mon Sep 17 00:00:00 2001 From: Arjun Satarkar Date: Thu, 3 Aug 2023 18:18:05 -0400 Subject: Handle more network errors --- serve.py | 15 ++++++++++++++- tagrss.py | 26 +++++++++++++------------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/serve.py b/serve.py index 3f2b722..eaa8bda 100755 --- a/serve.py +++ b/serve.py @@ -185,7 +185,15 @@ def add_feed_effect(): f'Cannot add feed with title "{str(e)}" as another feed already has that ' "title.", ) - # TODO: handle FeedFetchError too + except tagrss.FeedFetchError as e: + try: + if e.status_code != 200: + raise bottle.HTTPError( + 400, + f'Could not fetch feed: "{feed_source}" returned HTTP status code {e.status_code}.', + ) + except AttributeError: + raise bottle.HTTPError(500, f"Failed to fetch feed from {feed_source}.") return bottle.template( "add_feed", after_add=True, @@ -263,6 +271,11 @@ def update_feeds(run_event: threading.Event): for feed in feeds: try: core.update_feed(feed.id) # type: ignore + except tagrss.FeedFetchError as e: + logging.error( + f"Failed to update feed {feed.id} with source {feed.source} " + f"due to the following error: {e}." + ) except tagrss.StorageConstraintViolationError: logging.warning( f"Failed to update feed {feed.id} with source {feed.source} due" diff --git a/tagrss.py b/tagrss.py index 8c9ec81..ce67047 100644 --- a/tagrss.py +++ b/tagrss.py @@ -31,22 +31,19 @@ class FeedTitleAlreadyInUseError(StorageError): class StorageConstraintViolationError(StorageError): - def __init__(self, error): - super().__init__(error) - - -class SqliteMissingForeignKeySupportError(StorageError): pass - -class NetworkError(Exception): +class SqliteMissingForeignKeySupportError(StorageError): pass -class FeedFetchError(NetworkError): - def __init__(self, feed_source: str, status_code: int): - super().__init__(f"Get {feed_source} returned HTTP {status_code}") - +class FeedFetchError(Exception): + def __init__(self, *, feed_source: str, status_code: typing.Optional[int] = None, underlying: typing.Optional[Exception] = None): + 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 @@ -397,10 +394,13 @@ class TagRss: self.__storage = SqliteStorageProvider(storage_path) def __fetch_and_parse_feed(self, source) -> tuple[ParsedFeed, Epoch]: - response = requests.get(source) + try: + response = requests.get(source) + except requests.ConnectionError as e: + raise FeedFetchError(feed_source=source, underlying=e) epoch_downloaded: int = int(time.time()) if response.status_code != requests.codes.ok: - raise FeedFetchError(source, response.status_code) + raise FeedFetchError(feed_source=source, status_code=response.status_code) try: base: str = response.headers["Content-Location"] except KeyError: -- cgit v1.2.3-57-g22cb