Skip to content

Commit d985dc7

Browse files
author
Mike Dirolf
committed
handle non-zero response flags
1 parent 41a7f93 commit d985dc7

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

mongo.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,8 @@ def _refresh(self):
653653
"""Refreshes the cursor with more data from Mongo.
654654
655655
Returns the length of self.__data after refresh. Will exit early if
656-
self.__data is already non-empty.
656+
self.__data is already non-empty. Raises DatabaseException when the
657+
cursor cannot be refreshed due to an error on the query.
657658
"""
658659
if len(self.__data) or self.__killed:
659660
return len(self.__data)
@@ -663,8 +664,14 @@ def send_message(operation, message):
663664
request_id = self.__collection._send_message(operation, message)
664665
response = self.__collection.database()._receive_message(1, request_id)
665666

666-
# TODO handle non-zero response flags
667-
assert struct.unpack("<i", response[:4])[0] == 0
667+
response_flag = struct.unpack("<i", response[:4])[0]
668+
if response_flag == 1:
669+
raise DatabaseException("cursor id '%s' not valid at server" % self.__id)
670+
elif response_flag == 2:
671+
error_object = bson.to_dict(response[20:])
672+
raise DatabaseException("database error: %s" % error_object["$err"])
673+
else:
674+
assert response_flag == 0
668675

669676
self.__id = struct.unpack("<q", response[4:12])[0]
670677
assert struct.unpack("<i", response[12:16])[0] == self.__retrieved

0 commit comments

Comments
 (0)