# Path: apymongo/errors.py
# class AutoReconnect(ConnectionFailure):
#     """Raised when a connection to the database is lost and an attempt to
#     auto-reconnect will be made.
# 
#     In order to auto-reconnect you must handle this exception, recognizing that
#     the operation which caused it has not necessarily succeeded. Future
#     operations will attempt to open a new connection to the database (and
#     will continue to raise this exception until the first successful
#     connection is made).
#     """
# 
# class OperationFailure(PyMongoError):
#     """Raised when a database operation fails.
# 
#     .. versionadded:: 1.8
#        The :attr:`code` attribute.
#     """
# 
#     def __init__(self, error, code=None):
#         self.code = code
#         PyMongoError.__init__(self, error)
# 
# class TimeoutError(OperationFailure):
#     """Raised when a database operation times out.
# 
#     .. versionadded:: 1.8
#     """


    import hashlib
    import md5
import struct
import bson
import pymongo
import apymongo
from bson.son import SON
from apymongo.errors import (AutoReconnect,
                            OperationFailure,
                            TimeoutError)
    if response_flag & 1:
        # Shouldn't get this response if we aren't doing a getMore
        assert cursor_id is not None

        raise OperationFailure("cursor id '%s' not valid at server" %
                               cursor_id)
    elif response_flag & 2:
        error_object = bson.BSON(response[20:]).decode()
        if error_object["$err"] == "not master":
            raise AutoReconnect("master has changed")
        raise OperationFailure("database error: %s" %
                               error_object["$err"])

    result = {}
    result["cursor_id"] = struct.unpack("<q", response[4:12])[0]
    result["starting_from"] = struct.unpack("<i", response[12:16])[0]
    result["number_returned"] = struct.unpack("<i", response[16:20])[0]
    result["data"] = bson.decode_all(response[20:], as_class, tz_aware)
    assert len(result["data"]) == result["number_returned"]
    return result


def _check_command_response(response, reset, msg="%s", allowable_errors=[]):
    if not response["ok"]:
        if "wtimeout" in response and response["wtimeout"]:
            return TimeoutError(msg % response["errmsg"])
        if not response["errmsg"] in allowable_errors:
            if response["errmsg"] == "not master":
                reset()
                return AutoReconnect("not master")

# Path: apymongo/errors.py
# class AutoReconnect(ConnectionFailure):
#     """Raised when a connection to the database is lost and an attempt to
#     auto-reconnect will be made.
# 
#     In order to auto-reconnect you must handle this exception, recognizing that
#     the operation which caused it has not necessarily succeeded. Future
#     operations will attempt to open a new connection to the database (and
#     will continue to raise this exception until the first successful
#     connection is made).
#     """
# 
# class OperationFailure(PyMongoError):
#     """Raised when a database operation fails.
# 
#     .. versionadded:: 1.8
#        The :attr:`code` attribute.
#     """
# 
#     def __init__(self, error, code=None):
#         self.code = code
#         PyMongoError.__init__(self, error)
# 
# class TimeoutError(OperationFailure):
#     """Raised when a database operation times out.
# 
#     .. versionadded:: 1.8
#     """


# Path: apymongo/helpers.py
    import hashlib
    import md5
import struct
import bson
import pymongo
import apymongo
from bson.son import SON
from apymongo.errors import (AutoReconnect,
                            OperationFailure,
                            TimeoutError)

    if response_flag & 1:
        # Shouldn't get this response if we aren't doing a getMore
        assert cursor_id is not None

        raise OperationFailure("cursor id '%s' not valid at server" %
                               cursor_id)
    elif response_flag & 2:
        error_object = bson.BSON(response[20:]).decode()
        if error_object["$err"] == "not master":
            raise AutoReconnect("master has changed")
        raise OperationFailure("database error: %s" %
                               error_object["$err"])

    result = {}
    result["cursor_id"] = struct.unpack("<q", response[4:12])[0]
    result["starting_from"] = struct.unpack("<i", response[12:16])[0]
    result["number_returned"] = struct.unpack("<i", response[16:20])[0]
    result["data"] = bson.decode_all(response[20:], as_class, tz_aware)
    assert len(result["data"]) == result["number_returned"]
    return result


def _check_command_response(response, reset, msg="%s", allowable_errors=[]):
    if not response["ok"]:
        if "wtimeout" in response and response["wtimeout"]:
            return TimeoutError(msg % response["errmsg"])
        if not response["errmsg"] in allowable_errors:
            if response["errmsg"] == "not master":
                reset()
                return AutoReconnect("not master")

            return OperationFailure(msg % response["errmsg"])