WOrking on revamping the loader exceptions again.

This commit is contained in:
Michael Lazar
2015-12-07 01:12:09 -08:00
parent 6fa431aec9
commit 3a117f42af

View File

@@ -12,10 +12,10 @@ import threading
from contextlib import contextmanager from contextlib import contextmanager
import six import six
from praw.errors import PRAWException import praw
from requests import RequestException import requests
from .exceptions import RTVError from . import exceptions
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@@ -111,6 +111,13 @@ class LoadScreen(object):
>>> assert isinstance(terminal.loader.exception, KeyboardInterrupt) >>> assert isinstance(terminal.loader.exception, KeyboardInterrupt)
""" """
EXCEPTION_MESSAGES = [
# These ones are triggered by us, so they should always have a message
(exceptions.RTVError, '{0}'),
(praw.errors.PRAWException, '{0.__class__}'),
(requests.exceptions.RequestException, '{0.__class__}'),
]
def __init__(self, terminal): def __init__(self, terminal):
self.exception = None self.exception = None
@@ -180,19 +187,15 @@ class LoadScreen(object):
exc_name = type(e).__name__ exc_name = type(e).__name__
_logger.info('Loader caught: {0} - {1}'.format(exc_name, e)) _logger.info('Loader caught: {0} - {1}'.format(exc_name, e))
if isinstance(e, KeyboardInterrupt):
# Don't need to print anything for this one, just swallow it
return True
for e_type, message in self.EXCEPTION_MESSAGES:
# Some exceptions we want to swallow and display a notification # Some exceptions we want to swallow and display a notification
handled_exceptions = (RTVError, PRAWException, RequestException) if isinstance(e, e_type):
if isinstance(e, handled_exceptions): self._terminal.show_notification(message.format(e))
# Pass the message straight through to the user
message = six.text_type(e).split('/n') if str(e) else exc_name
self._terminal.show_notification(message)
return True return True
elif isinstance(e, KeyboardInterrupt):
# Don't need to print anything for this one
return True
else:
# Allow the exception to re-raise
return None
def animate(self, delay, interval, message, trail): def animate(self, delay, interval, message, trail):