errors.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # Copyright 2009-2015 MongoDB, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Exceptions raised by PyMongo."""
  15. from bson.errors import *
  16. try:
  17. from ssl import CertificateError
  18. except ImportError:
  19. from pymongo.ssl_match_hostname import CertificateError
  20. class PyMongoError(Exception):
  21. """Base class for all PyMongo exceptions."""
  22. class ConnectionFailure(PyMongoError):
  23. """Raised when a connection to the database cannot be made or is lost."""
  24. class AutoReconnect(ConnectionFailure):
  25. """Raised when a connection to the database is lost and an attempt to
  26. auto-reconnect will be made.
  27. In order to auto-reconnect you must handle this exception, recognizing that
  28. the operation which caused it has not necessarily succeeded. Future
  29. operations will attempt to open a new connection to the database (and
  30. will continue to raise this exception until the first successful
  31. connection is made).
  32. Subclass of :exc:`~pymongo.errors.ConnectionFailure`.
  33. """
  34. def __init__(self, message='', errors=None):
  35. self.errors = errors or []
  36. ConnectionFailure.__init__(self, message)
  37. class NetworkTimeout(AutoReconnect):
  38. """An operation on an open connection exceeded socketTimeoutMS.
  39. The remaining connections in the pool stay open. In the case of a write
  40. operation, you cannot know whether it succeeded or failed.
  41. Subclass of :exc:`~pymongo.errors.AutoReconnect`.
  42. """
  43. class NotMasterError(AutoReconnect):
  44. """The server responded "not master" or "node is recovering".
  45. These errors result from a query, write, or command. The operation failed
  46. because the client thought it was using the primary but the primary has
  47. stepped down, or the client thought it was using a healthy secondary but
  48. the secondary is stale and trying to recover.
  49. The client launches a refresh operation on a background thread, to update
  50. its view of the server as soon as possible after throwing this exception.
  51. Subclass of :exc:`~pymongo.errors.AutoReconnect`.
  52. """
  53. class ServerSelectionTimeoutError(AutoReconnect):
  54. """Thrown when no MongoDB server is available for an operation
  55. If there is no suitable server for an operation PyMongo tries for
  56. ``serverSelectionTimeoutMS`` (default 30 seconds) to find one, then
  57. throws this exception. For example, it is thrown after attempting an
  58. operation when PyMongo cannot connect to any server, or if you attempt
  59. an insert into a replica set that has no primary and does not elect one
  60. within the timeout window, or if you attempt to query with a Read
  61. Preference that the replica set cannot satisfy.
  62. """
  63. class ConfigurationError(PyMongoError):
  64. """Raised when something is incorrectly configured.
  65. """
  66. class OperationFailure(PyMongoError):
  67. """Raised when a database operation fails.
  68. .. versionadded:: 2.7
  69. The :attr:`details` attribute.
  70. """
  71. def __init__(self, error, code=None, details=None):
  72. self.__code = code
  73. self.__details = details
  74. PyMongoError.__init__(self, error)
  75. @property
  76. def code(self):
  77. """The error code returned by the server, if any.
  78. """
  79. return self.__code
  80. @property
  81. def details(self):
  82. """The complete error document returned by the server.
  83. Depending on the error that occurred, the error document
  84. may include useful information beyond just the error
  85. message. When connected to a mongos the error document
  86. may contain one or more subdocuments if errors occurred
  87. on multiple shards.
  88. """
  89. return self.__details
  90. class CursorNotFound(OperationFailure):
  91. """Raised while iterating query results if the cursor is
  92. invalidated on the server.
  93. .. versionadded:: 2.7
  94. """
  95. class ExecutionTimeout(OperationFailure):
  96. """Raised when a database operation times out, exceeding the $maxTimeMS
  97. set in the query or command option.
  98. .. note:: Requires server version **>= 2.6.0**
  99. .. versionadded:: 2.7
  100. """
  101. class WriteConcernError(OperationFailure):
  102. """Base exception type for errors raised due to write concern.
  103. .. versionadded:: 3.0
  104. """
  105. class WriteError(OperationFailure):
  106. """Base exception type for errors raised during write operations.
  107. .. versionadded:: 3.0
  108. """
  109. class WTimeoutError(WriteConcernError):
  110. """Raised when a database operation times out (i.e. wtimeout expires)
  111. before replication completes.
  112. With newer versions of MongoDB the `details` attribute may include
  113. write concern fields like 'n', 'updatedExisting', or 'writtenTo'.
  114. .. versionadded:: 2.7
  115. """
  116. class DuplicateKeyError(WriteError):
  117. """Raised when an insert or update fails due to a duplicate key error."""
  118. class BulkWriteError(OperationFailure):
  119. """Exception class for bulk write errors.
  120. .. versionadded:: 2.7
  121. """
  122. def __init__(self, results):
  123. OperationFailure.__init__(
  124. self, "batch op errors occurred", 65, results)
  125. class InvalidOperation(PyMongoError):
  126. """Raised when a client attempts to perform an invalid operation."""
  127. class InvalidName(PyMongoError):
  128. """Raised when an invalid name is used."""
  129. class CollectionInvalid(PyMongoError):
  130. """Raised when collection validation fails."""
  131. class InvalidURI(ConfigurationError):
  132. """Raised when trying to parse an invalid mongodb URI."""
  133. class ExceededMaxWaiters(Exception):
  134. """Raised when a thread tries to get a connection from a pool and
  135. ``maxPoolSize * waitQueueMultiple`` threads are already waiting.
  136. .. versionadded:: 2.6
  137. """
  138. pass
  139. class DocumentTooLarge(InvalidDocument):
  140. """Raised when an encoded document is too large for the connected server.
  141. """
  142. pass