From: W. Trevor King Date: Wed, 23 Jan 2013 14:13:43 +0000 (-0500) Subject: feeds: Raise an RSS2EmailError on invalid Feeds.index() arguments X-Git-Tag: v3.0~17 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=933dc46f6af3fb5473da93e02c9a90c1e4b4ae5f;p=rss2email.git feeds: Raise an RSS2EmailError on invalid Feeds.index() arguments Don't confuse non-Python folks by giving a traceback for this usage error. Reported-by: Dmitry Bogatov Signed-off-by: W. Trevor King --- diff --git a/rss2email/error.py b/rss2email/error.py index 276fa42..239361b 100644 --- a/rss2email/error.py +++ b/rss2email/error.py @@ -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' diff --git a/rss2email/feeds.py b/rss2email/feeds.py index 1f13e25..4a49974 100644 --- a/rss2email/feeds.py +++ b/rss2email/feeds.py @@ -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)