Change exceptions slightly

This commit is contained in:
Lorenz Diener 2017-11-24 14:20:27 +01:00
pare fa53713abd
commit cea4d4251a
S'han modificat 3 arxius amb 15 adicions i 13 eliminacions

Veure arxiu

@ -1419,6 +1419,7 @@ class MastodonError(Exception):
class MastodonIllegalArgumentError(ValueError, MastodonError): class MastodonIllegalArgumentError(ValueError, MastodonError):
"""Raised when an incorrect parameter is passed to a function"""
pass pass
@ -1427,16 +1428,24 @@ class MastodonIOError(IOError, MastodonError):
class MastodonFileNotFoundError(MastodonIOError): class MastodonFileNotFoundError(MastodonIOError):
"""Raised when a file requested to be loaded can not be opened"""
pass pass
class MastodonNetworkError(MastodonIOError): class MastodonNetworkError(MastodonIOError):
"""Raised when network communication with the server fails"""
pass pass
class MastodonAPIError(MastodonError): class MastodonAPIError(MastodonError):
"""Raised when the mastodon API generates a response that cannot be handled"""
pass pass
class MastodonRatelimitError(MastodonError): class MastodonRatelimitError(MastodonError):
"""Raised when rate limiting is set to manual mode and the rate limit is exceeded"""
pass
class MastodonMalformedEventError(MastodonError):
"""Raised when the server-sent event stream is malformed"""
pass pass

Veure arxiu

@ -1,4 +1,4 @@
from mastodon.Mastodon import Mastodon from mastodon.Mastodon import Mastodon
from mastodon.streaming import StreamListener, MalformedEventError from mastodon.streaming import StreamListener
__all__ = ['Mastodon', 'StreamListener', 'MalformedEventError'] __all__ = ['Mastodon', 'StreamListener']

Veure arxiu

@ -4,16 +4,9 @@ https://github.com/tootsuite/mastodon/blob/master/docs/Using-the-API/Streaming-A
""" """
import json import json
import logging
import six import six
from mastodon import Mastodon from mastodon import Mastodon
from mastodon.Mastodon import MastodonMalformedEventError
log = logging.getLogger(__name__)
class MalformedEventError(Exception):
"""Raised when the server-sent event stream is malformed."""
pass
class StreamListener(object): class StreamListener(object):
"""Callbacks for the streaming API. Create a subclass, override the on_xxx """Callbacks for the streaming API. Create a subclass, override the on_xxx
@ -55,7 +48,7 @@ class StreamListener(object):
line = raw_line.decode('utf-8') line = raw_line.decode('utf-8')
except UnicodeDecodeError as err: except UnicodeDecodeError as err:
six.raise_from( six.raise_from(
MalformedEventError("Malformed UTF-8", line), MastodonMalformedEventError("Malformed UTF-8", line),
err err
) )
@ -85,14 +78,14 @@ class StreamListener(object):
payload = json.loads(data, object_hook = Mastodon._Mastodon__json_hooks) payload = json.loads(data, object_hook = Mastodon._Mastodon__json_hooks)
except KeyError as err: except KeyError as err:
six.raise_from( six.raise_from(
MalformedEventError('Missing field', err.args[0], event), MastodonMalformedEventError('Missing field', err.args[0], event),
err err
) )
except ValueError as err: except ValueError as err:
# py2: plain ValueError # py2: plain ValueError
# py3: json.JSONDecodeError, a subclass of ValueError # py3: json.JSONDecodeError, a subclass of ValueError
six.raise_from( six.raise_from(
MalformedEventError('Bad JSON', data), MastodonMalformedEventError('Bad JSON', data),
err err
) )