exceptions.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. from enum import Enum
  2. "Core exceptions raised by the Redis client"
  3. class ExceptionType(Enum):
  4. NETWORK = "network"
  5. TLS = "tls"
  6. AUTH = "auth"
  7. SERVER = "server"
  8. class RedisError(Exception):
  9. def __init__(self, *args, status_code: str = None):
  10. super().__init__(*args)
  11. self.error_type = ExceptionType.SERVER
  12. self.status_code = status_code
  13. def __repr__(self):
  14. return f"{self.error_type.value}:{self.__class__.__name__}"
  15. class ConnectionError(RedisError):
  16. def __init__(self, *args, status_code: str = None):
  17. super().__init__(*args, status_code=status_code)
  18. self.error_type = ExceptionType.NETWORK
  19. class TimeoutError(RedisError):
  20. def __init__(self, *args, status_code: str = None):
  21. super().__init__(*args, status_code=status_code)
  22. self.error_type = ExceptionType.NETWORK
  23. class AuthenticationError(ConnectionError):
  24. def __init__(self, *args, status_code: str = None):
  25. super().__init__(*args, status_code=status_code)
  26. self.error_type = ExceptionType.AUTH
  27. class AuthorizationError(ConnectionError):
  28. def __init__(self, *args, status_code: str = None):
  29. super().__init__(*args, status_code=status_code)
  30. self.error_type = ExceptionType.AUTH
  31. class BusyLoadingError(ConnectionError):
  32. def __init__(self, *args, status_code: str = None):
  33. super().__init__(*args, status_code=status_code)
  34. self.error_type = ExceptionType.NETWORK
  35. class InvalidResponse(RedisError):
  36. pass
  37. class ResponseError(RedisError):
  38. pass
  39. class DataError(RedisError):
  40. pass
  41. class PubSubError(RedisError):
  42. pass
  43. class WatchError(RedisError):
  44. pass
  45. class NoScriptError(ResponseError):
  46. pass
  47. class OutOfMemoryError(ResponseError):
  48. """
  49. Indicates the database is full. Can only occur when either:
  50. * Redis maxmemory-policy=noeviction
  51. * Redis maxmemory-policy=volatile* and there are no evictable keys
  52. For more information see `Memory optimization in Redis <https://redis.io/docs/management/optimization/memory-optimization/#memory-allocation>`_. # noqa
  53. """
  54. pass
  55. class ExecAbortError(ResponseError):
  56. pass
  57. class ReadOnlyError(ResponseError):
  58. pass
  59. class NoPermissionError(ResponseError):
  60. def __init__(self, *args, status_code: str = None):
  61. super().__init__(*args, status_code=status_code)
  62. self.error_type = ExceptionType.AUTH
  63. class ModuleError(ResponseError):
  64. pass
  65. class LockError(RedisError, ValueError):
  66. "Errors acquiring or releasing a lock"
  67. # NOTE: For backwards compatibility, this class derives from ValueError.
  68. # This was originally chosen to behave like threading.Lock.
  69. def __init__(self, message=None, lock_name=None):
  70. super().__init__(message)
  71. self.message = message
  72. self.lock_name = lock_name
  73. class LockNotOwnedError(LockError):
  74. "Error trying to extend or release a lock that is not owned (anymore)"
  75. pass
  76. class ChildDeadlockedError(Exception):
  77. "Error indicating that a child process is deadlocked after a fork()"
  78. pass
  79. class AuthenticationWrongNumberOfArgsError(ResponseError):
  80. """
  81. An error to indicate that the wrong number of args
  82. were sent to the AUTH command
  83. """
  84. def __init__(self, *args, status_code: str = None):
  85. super().__init__(*args, status_code=status_code)
  86. self.error_type = ExceptionType.AUTH
  87. class RedisClusterException(Exception):
  88. """
  89. Base exception for the RedisCluster client
  90. """
  91. def __init__(self, *args):
  92. super().__init__(*args)
  93. self.error_type = ExceptionType.SERVER
  94. def __repr__(self):
  95. return f"{self.error_type.value}:{self.__class__.__name__}"
  96. class ClusterError(RedisError):
  97. """
  98. Cluster errors occurred multiple times, resulting in an exhaustion of the
  99. command execution TTL
  100. """
  101. def __init__(self, *args, status_code: str = None):
  102. super().__init__(*args, status_code=status_code)
  103. self.error_type = ExceptionType.SERVER
  104. class ClusterDownError(ClusterError, ResponseError):
  105. """
  106. Error indicated CLUSTERDOWN error received from cluster.
  107. By default Redis Cluster nodes stop accepting queries if they detect there
  108. is at least a hash slot uncovered (no available node is serving it).
  109. This way if the cluster is partially down (for example a range of hash
  110. slots are no longer covered) the entire cluster eventually becomes
  111. unavailable. It automatically returns available as soon as all the slots
  112. are covered again.
  113. """
  114. def __init__(self, resp, status_code: str = None):
  115. self.args = (resp,)
  116. self.message = resp
  117. self.error_type = ExceptionType.SERVER
  118. self.status_code = status_code
  119. class AskError(ResponseError):
  120. """
  121. Error indicated ASK error received from cluster.
  122. When a slot is set as MIGRATING, the node will accept all queries that
  123. pertain to this hash slot, but only if the key in question exists,
  124. otherwise the query is forwarded using a -ASK redirection to the node that
  125. is target of the migration.
  126. src node: MIGRATING to dst node
  127. get > ASK error
  128. ask dst node > ASKING command
  129. dst node: IMPORTING from src node
  130. asking command only affects next command
  131. any op will be allowed after asking command
  132. """
  133. def __init__(self, resp, status_code: str = None):
  134. """should only redirect to master node"""
  135. super().__init__(resp, status_code=status_code)
  136. self.args = (resp,)
  137. self.message = resp
  138. slot_id, new_node = resp.split(" ")
  139. host, port = new_node.rsplit(":", 1)
  140. self.slot_id = int(slot_id)
  141. self.node_addr = self.host, self.port = host, int(port)
  142. class TryAgainError(ResponseError):
  143. """
  144. Error indicated TRYAGAIN error received from cluster.
  145. Operations on keys that don't exist or are - during resharding - split
  146. between the source and destination nodes, will generate a -TRYAGAIN error.
  147. """
  148. def __init__(self, *args, status_code: str = None, **kwargs):
  149. super().__init__(*args, status_code=status_code)
  150. class ClusterCrossSlotError(ResponseError):
  151. """
  152. Error indicated CROSSSLOT error received from cluster.
  153. A CROSSSLOT error is generated when keys in a request don't hash to the
  154. same slot.
  155. """
  156. message = "Keys in request don't hash to the same slot"
  157. def __init__(self, *args, status_code: str = None):
  158. super().__init__(*args, status_code=status_code)
  159. self.error_type = ExceptionType.SERVER
  160. class MovedError(AskError):
  161. """
  162. Error indicated MOVED error received from cluster.
  163. A request sent to a node that doesn't serve this key will be replayed with
  164. a MOVED error that points to the correct node.
  165. """
  166. pass
  167. class MasterDownError(ClusterDownError):
  168. """
  169. Error indicated MASTERDOWN error received from cluster.
  170. Link with MASTER is down and replica-serve-stale-data is set to 'no'.
  171. """
  172. pass
  173. class SlotNotCoveredError(RedisClusterException):
  174. """
  175. This error only happens in the case where the connection pool will try to
  176. fetch what node that is covered by a given slot.
  177. If this error is raised the client should drop the current node layout and
  178. attempt to reconnect and refresh the node layout again
  179. """
  180. pass
  181. class MaxConnectionsError(ConnectionError):
  182. """
  183. Raised when a connection pool has reached its max_connections limit.
  184. This indicates pool exhaustion rather than an actual connection failure.
  185. """
  186. pass
  187. class CrossSlotTransactionError(RedisClusterException):
  188. """
  189. Raised when a transaction or watch is triggered in a pipeline
  190. and not all keys or all commands belong to the same slot.
  191. """
  192. pass
  193. class InvalidPipelineStack(RedisClusterException):
  194. """
  195. Raised on unexpected response length on pipelines. This is
  196. most likely a handling error on the stack.
  197. """
  198. pass
  199. class ExternalAuthProviderError(ConnectionError):
  200. """
  201. Raised when an external authentication provider returns an error.
  202. """
  203. pass
  204. class IncorrectPolicyType(Exception):
  205. """
  206. Raised when a policy type isn't matching to any known policy types.
  207. """
  208. pass