From 9675fb4ba46a81ae5eea9a366ea645d873964f6a Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Mon, 17 Oct 2016 17:43:10 -0700 Subject: [PATCH 1/2] Adding subreddit name to a few error messages. --- rtv/content.py | 5 ++++- rtv/exceptions.py | 9 +++++++-- rtv/objects.py | 1 + tests/test_content.py | 5 +++-- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/rtv/content.py b/rtv/content.py index 8a4537d..8fb199c 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -429,7 +429,10 @@ class SubredditContent(Content): try: self.get(0) except IndexError: - raise exceptions.SubredditError('No submissions') + full_name = self.name + if self.order: + full_name += '/' + self.order + raise exceptions.NoSubmissionsError(full_name) @classmethod def from_name(cls, reddit, name, loader, order=None, query=None): diff --git a/rtv/exceptions.py b/rtv/exceptions.py index fd1432d..4cd582b 100644 --- a/rtv/exceptions.py +++ b/rtv/exceptions.py @@ -22,8 +22,13 @@ class SubmissionError(RTVError): "Submission could not be loaded" -class SubredditError(RTVError): - "Subreddit could not be reached" +class NoSubmissionsError(RTVError): + "No submissions for the given page" + + def __init__(self, name): + self.name = name + message = '`{0}` has no submissions'.format(name) + super(NoSubmissionsError, self).__init__(message) class SubscriptionError(RTVError): diff --git a/rtv/objects.py b/rtv/objects.py index 377751f..bfa18bb 100644 --- a/rtv/objects.py +++ b/rtv/objects.py @@ -120,6 +120,7 @@ class LoadScreen(object): (praw.errors.OAuthScopeRequired, 'Not logged in'), (praw.errors.LoginRequired, 'Not logged in'), (praw.errors.InvalidCaptcha, 'Error, captcha required'), + (praw.errors.InvalidSubreddit, '{0.args[0]}'), (praw.errors.PRAWException, '{0.__class__.__name__}'), (requests.exceptions.RequestException, '{0.__class__.__name__}'), ] diff --git a/tests/test_content.py b/tests/test_content.py index 8435808..69d9627 100644 --- a/tests/test_content.py +++ b/tests/test_content.py @@ -399,8 +399,9 @@ def test_content_subreddit_me(reddit, oauth, refresh_token, terminal): # If there is no submitted content, an error should be raised if terminal.loader.exception: - assert isinstance(terminal.loader.exception, exceptions.SubredditError) - + assert isinstance(terminal.loader.exception, + exceptions.NoSubmissionsError) + assert terminal.loader.exception.name == '/u/me' def test_content_subscription(reddit, terminal): From 7e942c915e63d04a090d73258ce60a75874f89d7 Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Mon, 17 Oct 2016 18:07:18 -0700 Subject: [PATCH 2/2] Adding tests. --- rtv/content.py | 12 ++++++------ tests/test_content.py | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/rtv/content.py b/rtv/content.py index 8fb199c..5609716 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -475,12 +475,12 @@ class SubredditContent(Content): elif len(parts) == 2: resource, resource_order = parts else: - raise InvalidSubreddit() + raise InvalidSubreddit('`{}` is an invalid format'.format(name)) if not resource: # Praw does not correctly handle empty strings # https://github.com/praw-dev/praw/issues/615 - raise InvalidSubreddit() + raise InvalidSubreddit('Subreddit cannot be empty') # If the order was explicitly passed in, it will take priority over # the order that was extracted from the name @@ -496,12 +496,12 @@ class SubredditContent(Content): period = None if order not in ['hot', 'top', 'rising', 'new', 'controversial', None]: - raise InvalidSubreddit('Invalid order "%s"' % order) + raise InvalidSubreddit('Invalid order `%s`' % order) if period not in ['all', 'day', 'hour', 'month', 'week', 'year', None]: - raise InvalidSubreddit('Invalid period "%s"' % period) + raise InvalidSubreddit('Invalid period `%s`' % period) if period and order not in ['top', 'controversial']: - raise InvalidSubreddit('"%s" order does not allow sorting by' - 'period' % order) + raise InvalidSubreddit('`%s` order does not allow sorting by' + ' period' % order) # On some objects, praw doesn't allow you to pass arguments for the # order and period. Instead you need to call special helper functions diff --git a/tests/test_content.py b/tests/test_content.py index 69d9627..523c826 100644 --- a/tests/test_content.py +++ b/tests/test_content.py @@ -344,7 +344,8 @@ def test_content_subreddit_from_name_invalid(prompt, reddit, terminal): with terminal.loader(): SubredditContent.from_name(reddit, prompt, terminal.loader) assert isinstance(terminal.loader.exception, praw.errors.InvalidSubreddit) - + # Must always have an argument because it gets displayed + assert terminal.loader.exception.args[0] args, ids = SUBREDDIT_SEARCH_QUERIES.values(), list(SUBREDDIT_SEARCH_QUERIES) @pytest.mark.parametrize('prompt,query', args, ids=ids)