feeds: Raise an RSS2EmailError on invalid Feeds.index() arguments
authorW. Trevor King <wking@tremily.us>
Wed, 23 Jan 2013 14:13:43 +0000 (09:13 -0500)
committerW. Trevor King <wking@tremily.us>
Wed, 23 Jan 2013 14:16:48 +0000 (09:16 -0500)
Don't confuse non-Python folks by giving a traceback for this usage
error.

Reported-by: Dmitry Bogatov <KAction@gnu.org>
Signed-off-by: W. Trevor King <wking@tremily.us>
rss2email/error.py
rss2email/feeds.py

index 276fa42d032413cc278a00fa2d3c6cd501f330f5..239361b898e1e17b3f2422a70e32b8f7d648d061 100644 (file)
@@ -207,6 +207,15 @@ class NoToEmailAddress (InvalidFeedConfig, FeedsError):
             "'r2e add name url emailaddress'.")
 
 
+class FeedIndexError (FeedsError, IndexError):
+    def __init__(self, index, message=None, **kwargs):
+        if message is None:
+            message = 'feed {!r} not found'.format(index)
+        super(FeedIndexError, self).__init__(
+            message=message, **kwargs)
+        self.index = index
+
+
 class OPMLReadError (RSS2EmailError):
     def __init__(self, **kwargs):
         message = 'error reading OPML'
index 1f13e258c476f4dc08252a7139083edbc0cce861..4a499747bf29d9c8b9517810f85ca3d7617d6824 100644 (file)
@@ -163,7 +163,10 @@ class Feeds (list):
 
     def index(self, index):
         if isinstance(index, int):
-            return self[index]
+            try:
+                return self[index]
+            except IndexError as e:
+                raise _error.FeedIndexError(index=index, feeds=self) from e
         elif isinstance(index, str):
             try:
                 index = int(index)
@@ -174,7 +177,10 @@ class Feeds (list):
             for feed in self:
                 if feed.name == index:
                     return feed
-        super(Feeds, self).index(index)
+        try:
+            super(Feeds, self).index(index)
+        except (IndexError, ValueError) as e:
+            raise _error.FeedIndexError(index=index, feeds=self) from e
 
     def remove(self, feed):
         super(Feeds, self).remove(feed)