Re-raise Chunked Encoding Errors as Network Errors

This commit is contained in:
Lorenz Diener 2018-02-20 14:04:17 +01:00
parent d98a3546a3
commit 86ec5d7eca

View File

@ -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,6 +44,7 @@ class StreamListener(object):
""" """
event = {} event = {}
line_buffer = bytearray() line_buffer = bytearray()
try:
for chunk in response.iter_content(chunk_size = 1): for chunk in response.iter_content(chunk_size = 1):
if chunk: if chunk:
if chunk == b'\n': if chunk == b'\n':
@ -61,6 +63,11 @@ class StreamListener(object):
line_buffer = bytearray() line_buffer = bytearray()
else: else:
line_buffer.extend(chunk) line_buffer.extend(chunk)
except ChunkedEncodingError as err:
six.raise_from(
MastodonNetworkError("Server ceased communication."),
err
)
def _parse_line(self, line, event): def _parse_line(self, line, event):
if line.startswith(':'): if line.startswith(':'):