Re-raise Chunked Encoding Errors as Network Errors
This commit is contained in:
pare
d98a3546a3
commit
86ec5d7eca
|
@ -7,6 +7,7 @@ import json
|
||||||
import six
|
import six
|
||||||
from mastodon import Mastodon
|
from mastodon import Mastodon
|
||||||
from mastodon.Mastodon import MastodonMalformedEventError
|
from mastodon.Mastodon import MastodonMalformedEventError
|
||||||
|
from requests.exceptions import ChunkedEncodingError
|
||||||
|
|
||||||
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
|
||||||
|
@ -43,25 +44,31 @@ class StreamListener(object):
|
||||||
"""
|
"""
|
||||||
event = {}
|
event = {}
|
||||||
line_buffer = bytearray()
|
line_buffer = bytearray()
|
||||||
for chunk in response.iter_content(chunk_size = 1):
|
try:
|
||||||
if chunk:
|
for chunk in response.iter_content(chunk_size = 1):
|
||||||
if chunk == b'\n':
|
if chunk:
|
||||||
try:
|
if chunk == b'\n':
|
||||||
line = line_buffer.decode('utf-8')
|
try:
|
||||||
except UnicodeDecodeError as err:
|
line = line_buffer.decode('utf-8')
|
||||||
six.raise_from(
|
except UnicodeDecodeError as err:
|
||||||
MastodonMalformedEventError("Malformed UTF-8"),
|
six.raise_from(
|
||||||
err
|
MastodonMalformedEventError("Malformed UTF-8"),
|
||||||
)
|
err
|
||||||
if line == '':
|
)
|
||||||
self._dispatch(event)
|
if line == '':
|
||||||
event = {}
|
self._dispatch(event)
|
||||||
|
event = {}
|
||||||
|
else:
|
||||||
|
event = self._parse_line(line, event)
|
||||||
|
line_buffer = bytearray()
|
||||||
else:
|
else:
|
||||||
event = self._parse_line(line, event)
|
line_buffer.extend(chunk)
|
||||||
line_buffer = bytearray()
|
except ChunkedEncodingError as err:
|
||||||
else:
|
six.raise_from(
|
||||||
line_buffer.extend(chunk)
|
MastodonNetworkError("Server ceased communication."),
|
||||||
|
err
|
||||||
|
)
|
||||||
|
|
||||||
def _parse_line(self, line, event):
|
def _parse_line(self, line, event):
|
||||||
if line.startswith(':'):
|
if line.startswith(':'):
|
||||||
self.handle_heartbeat()
|
self.handle_heartbeat()
|
||||||
|
|
Loading…
Referencia en una nova incidència