|
32 | 32 |
|
33 | 33 | from pymongo import helpers, message |
34 | 34 | from pymongo.common import MAX_MESSAGE_SIZE |
35 | | -from pymongo.errors import AutoReconnect, NotMasterError, OperationFailure |
| 35 | +from pymongo.errors import (AutoReconnect, |
| 36 | + NotMasterError, |
| 37 | + OperationFailure, |
| 38 | + ProtocolError) |
36 | 39 | from pymongo.read_concern import DEFAULT_READ_CONCERN |
37 | 40 |
|
38 | 41 | _UNPACK_INT = struct.Struct("<i").unpack |
@@ -121,21 +124,21 @@ def receive_message( |
121 | 124 | length = _UNPACK_INT(header[:4])[0] |
122 | 125 |
|
123 | 126 | actual_op = _UNPACK_INT(header[12:])[0] |
124 | | - assert operation == actual_op, ("wire protocol error: " |
125 | | - "unknown opcode %r" % (actual_op,)) |
| 127 | + if operation != actual_op: |
| 128 | + raise ProtocolError("Got opcode %r but expected " |
| 129 | + "%r" % (actual_op, operation)) |
126 | 130 | # No request_id for exhaust cursor "getMore". |
127 | 131 | if request_id is not None: |
128 | 132 | response_id = _UNPACK_INT(header[8:12])[0] |
129 | | - assert request_id == response_id, ( |
130 | | - "wire protocol error: got response id %r but expected %r" |
131 | | - % (response_id, request_id)) |
132 | | - |
133 | | - assert length > 16, ("wire protocol error: message length is shorter" |
134 | | - " than standard message header: %r" % (length,)) |
135 | | - |
136 | | - assert length <= max_message_size, ( |
137 | | - "wire protocol error: message length (%r) is larger than server max " |
138 | | - "message size (%r)" % (length, max_message_size)) |
| 133 | + if request_id != response_id: |
| 134 | + raise ProtocolError("Got response id %r but expected " |
| 135 | + "%r" % (response_id, request_id)) |
| 136 | + if length <= 16: |
| 137 | + raise ProtocolError("Message length (%r) not longer than standard " |
| 138 | + "message header size (16)" % (length,)) |
| 139 | + if length > max_message_size: |
| 140 | + raise ProtocolError("Message length (%r) is larger than server max " |
| 141 | + "message size (%r)" % (length, max_message_size)) |
139 | 142 |
|
140 | 143 | return _receive_data_on_socket(sock, length - 16) |
141 | 144 |
|
|
0 commit comments