From 933dc46f6af3fb5473da93e02c9a90c1e4b4ae5f Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 23 Jan 2013 09:13:43 -0500 Subject: [PATCH] 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 --- rss2email/error.py | 9 +++++++++ rss2email/feeds.py | 10 ++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) 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) -- 2.26.2