mirror of
				https://github.com/tanrax/RSSingle.git
				synced 2025-03-03 03:35:46 +01:00 
			
		
		
		
	Improve Exception handling, and catch explicit Exceptions
This commit improves Exception handling, and made some changes to try/catch blocks, where the `KeyError` is now explicitly caught. If in the future, we have reports of other Exceptions being caught, we can include those in the handling. For now, I've used BaseException for two try/catch blocks. I need to check what exceptions can happen there. Signed-off-by: Dom Rodriguez <shymega@shymega.org.uk>
This commit is contained in:
		
							
								
								
									
										25
									
								
								rssingle.py
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								rssingle.py
									
									
									
									
									
								
							| @@ -70,9 +70,8 @@ def init_feed() -> None: | |||||||
|         fg.link(href=get_url_from_feed(CONFIG), rel="self") |         fg.link(href=get_url_from_feed(CONFIG), rel="self") | ||||||
|         fg.subtitle(CONFIG["description"]) |         fg.subtitle(CONFIG["description"]) | ||||||
|         fg.language("en") |         fg.language("en") | ||||||
|     except: |     except BaseException: # find out what exceptions FeedGenerator can cause as well as KeyError. | ||||||
|         log.error("Error initialising the feed!") |         logging.exception("Error initialising the feed!") | ||||||
|         sys.exit(1) |  | ||||||
|  |  | ||||||
|     log.debug("Feed initialised!") |     log.debug("Feed initialised!") | ||||||
|  |  | ||||||
| @@ -85,7 +84,7 @@ def parse_rss_feed(url) -> feedparser.FeedParserDict: | |||||||
|     try: |     try: | ||||||
|         # Hopefully this should parse.. |         # Hopefully this should parse.. | ||||||
|         return feedparser.parse(url) |         return feedparser.parse(url) | ||||||
|     except Exception: |     except BaseException: # find out what exceptions .parse() call can cause. | ||||||
|         log.warning("Failed to parse RSS feed.") |         log.warning("Failed to parse RSS feed.") | ||||||
|         # Now, we could handle gracefully. |         # Now, we could handle gracefully. | ||||||
|  |  | ||||||
| @@ -108,21 +107,21 @@ def main(): | |||||||
|  |  | ||||||
|             try: |             try: | ||||||
|                 fe.id(entry["id"]) |                 fe.id(entry["id"]) | ||||||
|             except: |             except KeyError: | ||||||
|                 # Deifnitely weird... |                 # Definitely weird... | ||||||
|                 log.warning("Empty id attribute, defaulting..") |                 log.warning("Empty id attribute, defaulting..") | ||||||
|                 fe.id("about:blank") |                 fe.id("about:blank") | ||||||
|  |  | ||||||
|             try: |             try: | ||||||
|                 fe.title(entry["title"]) |                 fe.title(entry["title"]) | ||||||
|             except: |             except KeyError: | ||||||
|                 # OK, this is a definite malformed feed! |                 # OK, this is a definite malformed feed! | ||||||
|                 log.warning("Empty title attribute, defaulting..") |                 log.warning("Empty title attribute, defaulting..") | ||||||
|                 fe.title("Unspecified") |                 fe.title("Unspecified") | ||||||
|  |  | ||||||
|             try: |             try: | ||||||
|                 fe.link(href=entry["link"]) |                 fe.link(href=entry["link"]) | ||||||
|             except: |             except KeyError: | ||||||
|                 # When we have a empty link attribute, this isn't ideal |                 # When we have a empty link attribute, this isn't ideal | ||||||
|                 # to set a default value.. :/ |                 # to set a default value.. :/ | ||||||
|                 log.warning("Empty link attribute, defaulting..") |                 log.warning("Empty link attribute, defaulting..") | ||||||
| @@ -136,12 +135,12 @@ def main(): | |||||||
|                     try: |                     try: | ||||||
|                         for author in entry["authors"]: |                         for author in entry["authors"]: | ||||||
|                             fe.author(author) |                             fe.author(author) | ||||||
|                     except: |                     except KeyError: | ||||||
|                         log.debug("Oh dear, a malformed feed! Adjusting.") |                         log.debug("Oh dear, a malformed feed! Adjusting.") | ||||||
|                         # This is a ugly hack to fix broken feed entries with the author attribute! |                         # This is a ugly hack to fix broken feed entries with the author attribute! | ||||||
|                         author["email"] = author.pop("href") |                         author["email"] = author.pop("href") | ||||||
|                         fe.author(author) |                         fe.author(author) | ||||||
|             except: |             except KeyError: | ||||||
|                 # Sometimes we don't have ANY author attributes, so we |                 # Sometimes we don't have ANY author attributes, so we | ||||||
|                 # have to set a dummy attribute. |                 # have to set a dummy attribute. | ||||||
|                 log.warning("Empty authors attribute, defaulting..") |                 log.warning("Empty authors attribute, defaulting..") | ||||||
| @@ -155,7 +154,7 @@ def main(): | |||||||
|                     fe.description(entry["description"]) |                     fe.description(entry["description"]) | ||||||
|                     fe.summary(entry["description"]) |                     fe.summary(entry["description"]) | ||||||
|                     fe.content(entry["description"]) |                     fe.content(entry["description"]) | ||||||
|             except: |             except KeyError: | ||||||
|                 # Sometimes feeds don't provide a summary OR description, so we |                 # Sometimes feeds don't provide a summary OR description, so we | ||||||
|                 # have to set an empty value. |                 # have to set an empty value. | ||||||
|                 # This is pretty useless for a feed, so hopefully we |                 # This is pretty useless for a feed, so hopefully we | ||||||
| @@ -169,11 +168,11 @@ def main(): | |||||||
|                     try: |                     try: | ||||||
|                         fe.published(entry["published"]) |                         fe.published(entry["published"]) | ||||||
|                         fe.updated(entry["published"]) |                         fe.updated(entry["published"]) | ||||||
|                     except: |                     except KeyError: | ||||||
|                         fe.published("1970-01/01T00:00:00+00:00") |                         fe.published("1970-01/01T00:00:00+00:00") | ||||||
|                         fe.updated("1970-01/01T00:00:00+00:00") |                         fe.updated("1970-01/01T00:00:00+00:00") | ||||||
|                         continue |                         continue | ||||||
|             except: |             except KeyError: | ||||||
|                 # Sometimes feeds don't even provide a publish date, so we default to |                 # Sometimes feeds don't even provide a publish date, so we default to | ||||||
|                 # the start date &time of the Unix epoch. |                 # the start date &time of the Unix epoch. | ||||||
|                 log.warning("Empty publish attribute, defaulting..") |                 log.warning("Empty publish attribute, defaulting..") | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user