diff --git a/rtv/__main__.py b/rtv/__main__.py index ff95aa6..9934673 100644 --- a/rtv/__main__.py +++ b/rtv/__main__.py @@ -3,13 +3,14 @@ import sys import locale import logging -import requests import praw import praw.errors import tornado +import requests +from requests import exceptions from . import config -from .exceptions import SubmissionError, SubredditError, SubscriptionError, ProgramError +from .exceptions import RTVError from .curses_helpers import curses_session, LoadScreen from .submission import SubmissionPage from .subreddit import SubredditPage @@ -18,6 +19,7 @@ from .oauth import OAuthTool from .__version__ import __version__ __all__ = [] +_logger = logging.getLogger(__name__) # Pycharm debugging note: # You can use pycharm to debug a curses application by launching rtv in a @@ -34,11 +36,9 @@ def main(): locale.setlocale(locale.LC_ALL, '') # Set the terminal title + # TODO: Need to clear the title when the program exits title = 'rtv {0}'.format(__version__) - if os.name == 'nt': - os.system('title {0}'.format(title)) - else: - sys.stdout.write("\x1b]2;{0}\x07".format(title)) + sys.stdout.write("\x1b]2;{0}\x07".format(title)) # Fill in empty arguments with config file values. Parameters explicitly # typed on the command line will take priority over config file params. @@ -54,11 +54,14 @@ def main(): config.unicode = False if not args.persistent: config.persistent = False - if args.log: - logging.basicConfig(level=logging.DEBUG, filename=args.log) if args.clear_auth: config.clear_refresh_token() + if args.log: + logging.basicConfig(level=logging.DEBUG, filename=args.log) + else: + logging.root.addHandler(logging.NullHandler()) + try: print('Connecting...') reddit = praw.Reddit(user_agent=AGENT.format(version=__version__)) @@ -74,19 +77,9 @@ def main(): subreddit = args.subreddit or 'front' page = SubredditPage(stdscr, reddit, oauth, subreddit) page.loop() - except (praw.errors.OAuthAppRequired, praw.errors.OAuthInvalidToken): - print('Invalid OAuth data') - except praw.errors.NotFound: - print('HTTP Error: 404 Not Found') - except praw.errors.HTTPException: - print('Connection timeout') - except SubmissionError as e: - print('Could not reach submission URL: {}'.format(e.url)) - except SubredditError as e: - print('Could not reach subreddit: {}'.format(e.name)) - except ProgramError as e: - print('Error: could not open file with program "{}", ' - 'try setting RTV_EDITOR'.format(e.name)) + except (exceptions.RequestException, praw.errors.PRAWException, RTVError) as e: + _logger.exception(e) + print('{}: {}'.format(type(e).__name__, e)) except KeyboardInterrupt: pass finally: diff --git a/rtv/content.py b/rtv/content.py index 17c02a3..2f519df 100644 --- a/rtv/content.py +++ b/rtv/content.py @@ -194,7 +194,7 @@ class SubmissionContent(BaseContent): url = url.replace('http:', 'https:') submission = reddit.get_submission(url, comment_sort=order) except (praw.errors.APIException, praw.errors.NotFound): - raise SubmissionError(url) + raise SubmissionError('Could not load %s' % url) return cls(submission, loader, indent_size, max_indent_level, order) @@ -299,7 +299,7 @@ class SubredditContent(BaseContent): praw.errors.RedirectException, praw.errors.Forbidden, praw.errors.InvalidSubreddit, praw.errors.NotFound, IndexError): - raise SubredditError(name) + raise SubredditError('Could not reach subreddit %s' % name) @classmethod def from_name(cls, reddit, name, loader, order=None, query=None): @@ -317,11 +317,11 @@ class SubredditContent(BaseContent): display_name = '/r/{}'.format(name) if order not in ['hot', 'top', 'rising', 'new', 'controversial', None]: - raise SubredditError(name) + raise SubredditError('Unrecognized order "%s"' % order) if name == 'me': if not reddit.is_logged_in(): - raise AccountError + raise AccountError('Could not access user account') elif order: submissions = reddit.user.get_submitted(sort=order) else: @@ -403,7 +403,7 @@ class SubscriptionContent(BaseContent): with loader(): subscriptions = reddit.get_my_subreddits(limit=None) except praw.errors.APIException: - raise SubscriptionError() + raise SubscriptionError('Unable to load subscriptions') return cls(subscriptions, loader) diff --git a/rtv/exceptions.py b/rtv/exceptions.py index 80fb76f..3062a7a 100644 --- a/rtv/exceptions.py +++ b/rtv/exceptions.py @@ -13,16 +13,10 @@ class AccountError(RTVError): class SubmissionError(RTVError): "Submission could not be loaded" - def __init__(self, url): - self.url = url - class SubredditError(RTVError): "Subreddit could not be reached" - def __init__(self, name): - self.name = name - class SubscriptionError(RTVError): "Subscriptions could not be fetched" @@ -30,6 +24,3 @@ class SubscriptionError(RTVError): class ProgramError(RTVError): "Problem executing an external program" - - def __init__(self, name): - self.name = name diff --git a/rtv/helpers.py b/rtv/helpers.py index d8a73c1..09e22cc 100644 --- a/rtv/helpers.py +++ b/rtv/helpers.py @@ -70,7 +70,7 @@ def open_editor(data=''): try: subprocess.Popen([editor, fp.name]).wait() except OSError: - raise ProgramError(editor) + raise ProgramError('Could not open file with %s' % editor) curses.doupdate() # Open a second file object to read. This appears to be necessary in