From fccb4f705bb5ead64059a1f5994ef5e593095062 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sun, 7 Aug 2022 18:50:14 +0200 Subject: [PATCH] RSS: Log feed URL when feedparser.parse raises exceptions --- plugins/RSS/plugin.py | 13 +++++++++++-- plugins/RSS/test.py | 18 +++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/plugins/RSS/plugin.py b/plugins/RSS/plugin.py index ff8b15e4b..8c63c0350 100644 --- a/plugins/RSS/plugin.py +++ b/plugins/RSS/plugin.py @@ -356,8 +356,17 @@ class RSS(callbacks.Plugin): handlers.append(ProxyHandler( {'https': utils.force(utils.web.proxy())})) with feed.lock: - d = feedparser.parse(feed.url, etag=feed.etag, - modified=feed.modified, handlers=handlers) + try: + d = feedparser.parse(feed.url, etag=feed.etag, + modified=feed.modified, handlers=handlers) + except socket.error as e: + self.log.warning("Network error while fetching <%s>: %s", + feed.url, e) + feed.last_exception = e + return + except Exception as e: + self.log.error("Failed to fetch <%s>: %s", feed.url, e) + raise # reraise so @log.firewall prints the traceback if 'status' not in d or d.status != 304: # Not modified if 'etag' in d: feed.etag = d.etag diff --git a/plugins/RSS/test.py b/plugins/RSS/test.py index 0ec8a9ad2..6de976a65 100644 --- a/plugins/RSS/test.py +++ b/plugins/RSS/test.py @@ -31,6 +31,7 @@ import functools from unittest.mock import patch +import socket import sys import feedparser @@ -362,7 +363,22 @@ class RSSTestCase(ChannelPluginTestCase): timeFastForward(1.1) mock._data = not_well_formed self.assertRegexp('rss http://example.com/', - 'Parser error') + 'Parser error: .*mismatch') + + def testSocketError(self): + class MockResponse: + headers = {} + url = '' + def read(self): + raise socket.error("oh no") + + def close(self): + pass + mock = MockResponse() + with patch("urllib.request.OpenerDirector.open", return_value=mock): + timeFastForward(1.1) + self.assertRegexp('rss http://example.com/', + 'Parser error: .*oh no') if network: timeout = 5 # Note this applies also to the above tests