From ec4a1d773609a922734f36bfe2360c7e622ff155 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Wed, 16 Nov 2016 19:09:15 -0300 Subject: [refactor] remove assert logic from fetch_protocol Asserts aren't a good solution for stream parsing, its cleaner to check and raise in place. Also, asserts can be ignored. --- .../leap/soledad/client/http_target/fetch_protocol.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/client/src/leap/soledad/client/http_target/fetch_protocol.py b/client/src/leap/soledad/client/http_target/fetch_protocol.py index 4290785d..a15991f3 100644 --- a/client/src/leap/soledad/client/http_target/fetch_protocol.py +++ b/client/src/leap/soledad/client/http_target/fetch_protocol.py @@ -90,11 +90,8 @@ class DocStreamReceiver(ReadBodyProtocol): lines = self.consumeBufferLines() while lines: line, _ = utils.check_and_strip_comma(lines.pop(0)) - try: - self.lineReceived(line) - self._line += 1 - except AssertionError, e: - raise errors.BrokenSyncStream(e) + self.lineReceived(line) + self._line += 1 def lineReceived(self, line): """ @@ -105,18 +102,22 @@ class DocStreamReceiver(ReadBodyProtocol): (odd): {data},\r\n (last): ] """ - assert not self._properly_finished + if self._properly_finished: + raise errors.BrokenSyncStream("Reading a finished stream") if ']' == line: self._properly_finished = True elif self._line == 0: - assert line == '[' + if line is not '[': + raise errors.BrokenSyncStream("Invalid start") elif self._line == 1: self.metadata = line - assert 'error' not in self.metadata + if 'error' in self.metadata: + raise errors.BrokenSyncStream("Error from server: %s" % line) self.total = json.loads(line).get('number_of_changes', -1) elif (self._line % 2) == 0: self.current_doc = json.loads(line) - assert 'error' not in self.current_doc + if 'error' in self.current_doc: + raise errors.BrokenSyncStream("Error from server: %s" % line) else: self._doc_reader( self.current_doc, line.strip() or None, self.total) -- cgit v1.2.3