diff options
author | Arjun Satarkar <me@arjunsatarkar.net> | 2023-08-03 22:18:05 +0000 |
---|---|---|
committer | Arjun Satarkar <me@arjunsatarkar.net> | 2023-08-03 22:18:05 +0000 |
commit | 353001c9258de2cd1f8fcc026ea954cae624f669 (patch) | |
tree | cb28b9c3eae4878e4c6e54af35e29861959042fb | |
parent | a45bcb74dde80ee576bfcbc2f3efb719e8f6b2d9 (diff) | |
download | tagrss-353001c9258de2cd1f8fcc026ea954cae624f669.tar tagrss-353001c9258de2cd1f8fcc026ea954cae624f669.tar.gz tagrss-353001c9258de2cd1f8fcc026ea954cae624f669.zip |
Handle more network errors
-rwxr-xr-x | serve.py | 15 | ||||
-rw-r--r-- | tagrss.py | 26 |
2 files changed, 27 insertions, 14 deletions
@@ -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" @@ -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: |