From 8f5488436b90d85969abb8bdcbb0ecf14d8936bd Mon Sep 17 00:00:00 2001 From: Arjun Satarkar Date: Fri, 4 Aug 2023 22:09:08 +0530 Subject: Handle if a non-feed URL is given --- README.md | 2 -- serve.py | 8 +++++++- tagrss.py | 10 ++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ece0e12..39b7e8b 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,6 @@ See `LICENSE.txt` in the root of this repository for the text of the license. ## To do * Add JS to make the feed/tag input situation work like one would normally expect rather than like it's 1985. (Progressive enhancement, though.) -* Do more user input validation -* Handle more `requests` and `feedparser` error conditions * Add support for authentication * Allow specifying update interval on a per-feed basis diff --git a/serve.py b/serve.py index 9ad398f..ee51c99 100755 --- a/serve.py +++ b/serve.py @@ -228,6 +228,12 @@ def add_feed_effect(): ) else: raise bottle.HTTPError(500, f"Failed to fetch feed from {feed_source}.") + except tagrss.NotAFeedError: + raise bottle.HTTPError( + 400, + f"Could not add feed from {feed_source} as the content at the location is " + "not a valid feed.", + ) return bottle.template( "add_feed", after_add=True, @@ -316,7 +322,7 @@ def update_feeds(run_event: threading.Event): for feed in feeds: try: core.update_feed(feed.id) - except tagrss.FeedFetchError as e: + except (tagrss.FeedFetchError, tagrss.NotAFeedError) as e: logging.error( f"Failed to update feed {feed.id} with source {feed.source} " f"due to the following error: {e}." diff --git a/tagrss.py b/tagrss.py index 9179035..ae9482c 100644 --- a/tagrss.py +++ b/tagrss.py @@ -59,6 +59,10 @@ class FeedFetchError(Exception): super().__init__(f"Get {feed_source} failed: {underlying}") +class NotAFeedError(Exception): + pass + + FeedId = int Epoch = int ParsedFeed = feedparser.FeedParserDict @@ -431,6 +435,12 @@ class TagRss: io.BytesIO(bytes(response.text, encoding="utf-8")), response_headers={"Content-Location": base}, ) + if not ( + getattr(parsed.feed, "title", None) + or getattr(parsed.feed, "link", None) + or getattr(parsed.feed, "id", None) + ): + raise NotAFeedError(source) return (parsed, epoch_downloaded) def add_feed( -- cgit v1.2.3-57-g22cb