Merge pull request #315 from michael-lazar/better_error_messages

Better error messages
This commit is contained in:
Michael Lazar
2016-10-17 19:23:43 -07:00
committed by GitHub
4 changed files with 23 additions and 12 deletions

View File

@@ -429,7 +429,10 @@ class SubredditContent(Content):
try: try:
self.get(0) self.get(0)
except IndexError: except IndexError:
raise exceptions.SubredditError('No submissions') full_name = self.name
if self.order:
full_name += '/' + self.order
raise exceptions.NoSubmissionsError(full_name)
@classmethod @classmethod
def from_name(cls, reddit, name, loader, order=None, query=None): def from_name(cls, reddit, name, loader, order=None, query=None):
@@ -472,12 +475,12 @@ class SubredditContent(Content):
elif len(parts) == 2: elif len(parts) == 2:
resource, resource_order = parts resource, resource_order = parts
else: else:
raise InvalidSubreddit() raise InvalidSubreddit('`{}` is an invalid format'.format(name))
if not resource: if not resource:
# Praw does not correctly handle empty strings # Praw does not correctly handle empty strings
# https://github.com/praw-dev/praw/issues/615 # 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 # If the order was explicitly passed in, it will take priority over
# the order that was extracted from the name # the order that was extracted from the name
@@ -493,12 +496,12 @@ class SubredditContent(Content):
period = None period = None
if order not in ['hot', 'top', 'rising', 'new', 'controversial', 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]: 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']: if period and order not in ['top', 'controversial']:
raise InvalidSubreddit('"%s" order does not allow sorting by' raise InvalidSubreddit('`%s` order does not allow sorting by'
'period' % order) ' period' % order)
# On some objects, praw doesn't allow you to pass arguments for the # On some objects, praw doesn't allow you to pass arguments for the
# order and period. Instead you need to call special helper functions # order and period. Instead you need to call special helper functions

View File

@@ -22,8 +22,13 @@ class SubmissionError(RTVError):
"Submission could not be loaded" "Submission could not be loaded"
class SubredditError(RTVError): class NoSubmissionsError(RTVError):
"Subreddit could not be reached" "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): class SubscriptionError(RTVError):

View File

@@ -120,6 +120,7 @@ class LoadScreen(object):
(praw.errors.OAuthScopeRequired, 'Not logged in'), (praw.errors.OAuthScopeRequired, 'Not logged in'),
(praw.errors.LoginRequired, 'Not logged in'), (praw.errors.LoginRequired, 'Not logged in'),
(praw.errors.InvalidCaptcha, 'Error, captcha required'), (praw.errors.InvalidCaptcha, 'Error, captcha required'),
(praw.errors.InvalidSubreddit, '{0.args[0]}'),
(praw.errors.PRAWException, '{0.__class__.__name__}'), (praw.errors.PRAWException, '{0.__class__.__name__}'),
(requests.exceptions.RequestException, '{0.__class__.__name__}'), (requests.exceptions.RequestException, '{0.__class__.__name__}'),
] ]

View File

@@ -344,7 +344,8 @@ def test_content_subreddit_from_name_invalid(prompt, reddit, terminal):
with terminal.loader(): with terminal.loader():
SubredditContent.from_name(reddit, prompt, terminal.loader) SubredditContent.from_name(reddit, prompt, terminal.loader)
assert isinstance(terminal.loader.exception, praw.errors.InvalidSubreddit) 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) args, ids = SUBREDDIT_SEARCH_QUERIES.values(), list(SUBREDDIT_SEARCH_QUERIES)
@pytest.mark.parametrize('prompt,query', args, ids=ids) @pytest.mark.parametrize('prompt,query', args, ids=ids)
@@ -399,8 +400,9 @@ def test_content_subreddit_me(reddit, oauth, refresh_token, terminal):
# If there is no submitted content, an error should be raised # If there is no submitted content, an error should be raised
if terminal.loader.exception: 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): def test_content_subscription(reddit, terminal):