exceptions.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. """Pika specific exceptions"""
  2. # pylint: disable=C0111,E1136
  3. class AMQPError(Exception):
  4. def __repr__(self):
  5. return '%s: An unspecified AMQP error has occurred; %s' % (
  6. self.__class__.__name__, self.args)
  7. class AMQPConnectionError(AMQPError):
  8. def __repr__(self):
  9. if len(self.args) == 2:
  10. return '{}: ({}) {}'.format(self.__class__.__name__, self.args[0],
  11. self.args[1])
  12. else:
  13. return '{}: {}'.format(self.__class__.__name__, self.args)
  14. class ConnectionOpenAborted(AMQPConnectionError):
  15. """Client closed connection while opening."""
  16. class StreamLostError(AMQPConnectionError):
  17. """Stream (TCP) connection lost."""
  18. class IncompatibleProtocolError(AMQPConnectionError):
  19. def __repr__(self):
  20. return (
  21. '%s: The protocol returned by the server is not supported: %s' % (
  22. self.__class__.__name__,
  23. self.args,
  24. ))
  25. class AuthenticationError(AMQPConnectionError):
  26. def __repr__(self):
  27. return ('%s: Server and client could not negotiate use of the %s '
  28. 'authentication mechanism' % (self.__class__.__name__,
  29. self.args[0]))
  30. class ProbableAuthenticationError(AMQPConnectionError):
  31. def __repr__(self):
  32. return (
  33. '%s: Client was disconnected at a connection stage indicating a '
  34. 'probable authentication error: %s' % (
  35. self.__class__.__name__,
  36. self.args,
  37. ))
  38. class ProbableAccessDeniedError(AMQPConnectionError):
  39. def __repr__(self):
  40. return (
  41. '%s: Client was disconnected at a connection stage indicating a '
  42. 'probable denial of access to the specified virtual host: %s' % (
  43. self.__class__.__name__,
  44. self.args,
  45. ))
  46. class NoFreeChannels(AMQPConnectionError):
  47. def __repr__(self):
  48. return '%s: The connection has run out of free channels' % (
  49. self.__class__.__name__)
  50. class ConnectionWrongStateError(AMQPConnectionError):
  51. """Connection is in wrong state for the requested operation."""
  52. def __repr__(self):
  53. if self.args:
  54. return super(ConnectionWrongStateError, self).__repr__()
  55. else:
  56. return ('%s: The connection is in wrong state for the requested '
  57. 'operation.' % self.__class__.__name__)
  58. class ConnectionClosed(AMQPConnectionError):
  59. def __init__(self, reply_code, reply_text):
  60. """
  61. :param int reply_code: reply-code that was used in user's or broker's
  62. `Connection.Close` method. NEW in v1.0.0
  63. :param str reply_text: reply-text that was used in user's or broker's
  64. `Connection.Close` method. Human-readable string corresponding to
  65. `reply_code`. NEW in v1.0.0
  66. """
  67. super(ConnectionClosed, self).__init__(int(reply_code), str(reply_text))
  68. def __repr__(self):
  69. return '{}: ({}) {!r}'.format(self.__class__.__name__, self.reply_code,
  70. self.reply_text)
  71. @property
  72. def reply_code(self):
  73. """ NEW in v1.0.0
  74. :rtype: int
  75. """
  76. return self.args[0]
  77. @property
  78. def reply_text(self):
  79. """ NEW in v1.0.0
  80. :rtype: str
  81. """
  82. return self.args[1]
  83. class ConnectionClosedByBroker(ConnectionClosed):
  84. """Connection.Close from broker."""
  85. class ConnectionClosedByClient(ConnectionClosed):
  86. """Connection was closed at request of Pika client."""
  87. class ConnectionBlockedTimeout(AMQPConnectionError):
  88. """RabbitMQ-specific: timed out waiting for connection.unblocked."""
  89. class AMQPHeartbeatTimeout(AMQPConnectionError):
  90. """Connection was dropped as result of heartbeat timeout."""
  91. class AMQPChannelError(AMQPError):
  92. def __repr__(self):
  93. return '{}: {!r}'.format(self.__class__.__name__, self.args)
  94. class ChannelWrongStateError(AMQPChannelError):
  95. """Channel is in wrong state for the requested operation."""
  96. class ChannelClosed(AMQPChannelError):
  97. """The channel closed by client or by broker
  98. """
  99. def __init__(self, reply_code, reply_text):
  100. """
  101. :param int reply_code: reply-code that was used in user's or broker's
  102. `Channel.Close` method. One of the AMQP-defined Channel Errors.
  103. NEW in v1.0.0
  104. :param str reply_text: reply-text that was used in user's or broker's
  105. `Channel.Close` method. Human-readable string corresponding to
  106. `reply_code`;
  107. NEW in v1.0.0
  108. """
  109. super(ChannelClosed, self).__init__(int(reply_code), str(reply_text))
  110. def __repr__(self):
  111. return '{}: ({}) {!r}'.format(self.__class__.__name__, self.reply_code,
  112. self.reply_text)
  113. @property
  114. def reply_code(self):
  115. """ NEW in v1.0.0
  116. :rtype: int
  117. """
  118. return self.args[0]
  119. @property
  120. def reply_text(self):
  121. """ NEW in v1.0.0
  122. :rtype: str
  123. """
  124. return self.args[1]
  125. class ChannelClosedByBroker(ChannelClosed):
  126. """`Channel.Close` from broker; may be passed as reason to channel's
  127. on-closed callback of non-blocking connection adapters or raised by
  128. `BlockingConnection`.
  129. NEW in v1.0.0
  130. """
  131. class ChannelClosedByClient(ChannelClosed):
  132. """Channel closed by client upon receipt of `Channel.CloseOk`; may be passed
  133. as reason to channel's on-closed callback of non-blocking connection
  134. adapters, but not raised by `BlockingConnection`.
  135. NEW in v1.0.0
  136. """
  137. class DuplicateConsumerTag(AMQPChannelError):
  138. def __repr__(self):
  139. return ('%s: The consumer tag specified already exists for this '
  140. 'channel: %s' % (self.__class__.__name__, self.args[0]))
  141. class ConsumerCancelled(AMQPChannelError):
  142. def __repr__(self):
  143. return '%s: Server cancelled consumer' % self.__class__.__name__
  144. class UnroutableError(AMQPChannelError):
  145. """Exception containing one or more unroutable messages returned by broker
  146. via Basic.Return.
  147. Used by BlockingChannel.
  148. In publisher-acknowledgements mode, this is raised upon receipt of Basic.Ack
  149. from broker; in the event of Basic.Nack from broker, `NackError` is raised
  150. instead
  151. """
  152. def __init__(self, messages):
  153. """
  154. :param sequence(blocking_connection.ReturnedMessage) messages: Sequence
  155. of returned unroutable messages
  156. """
  157. super(UnroutableError, self).__init__(
  158. "%s unroutable message(s) returned" % (len(messages)))
  159. self.messages = messages
  160. def __repr__(self):
  161. return '%s: %i unroutable messages returned by broker' % (
  162. self.__class__.__name__, len(self.messages))
  163. class NackError(AMQPChannelError):
  164. """This exception is raised when a message published in
  165. publisher-acknowledgements mode is Nack'ed by the broker.
  166. Used by BlockingChannel.
  167. """
  168. def __init__(self, messages):
  169. """
  170. :param sequence(blocking_connection.ReturnedMessage) messages: Sequence
  171. of returned unroutable messages
  172. """
  173. super(NackError,
  174. self).__init__("%s message(s) NACKed" % (len(messages)))
  175. self.messages = messages
  176. def __repr__(self):
  177. return '%s: %i unroutable messages returned by broker' % (
  178. self.__class__.__name__, len(self.messages))
  179. class InvalidChannelNumber(AMQPError):
  180. def __repr__(self):
  181. return '%s: An invalid channel number has been specified: %s' % (
  182. self.__class__.__name__, self.args[0])
  183. class ProtocolSyntaxError(AMQPError):
  184. def __repr__(self):
  185. return '%s: An unspecified protocol syntax error occurred' % (
  186. self.__class__.__name__)
  187. class UnexpectedFrameError(ProtocolSyntaxError):
  188. def __repr__(self):
  189. return '%s: Received a frame out of sequence: %r' % (
  190. self.__class__.__name__, self.args[0])
  191. class ProtocolVersionMismatch(ProtocolSyntaxError):
  192. def __repr__(self):
  193. return '%s: Protocol versions did not match: %r vs %r' % (
  194. self.__class__.__name__, self.args[0], self.args[1])
  195. class BodyTooLongError(ProtocolSyntaxError):
  196. def __repr__(self):
  197. return ('%s: Received too many bytes for a message delivery: '
  198. 'Received %i, expected %i' % (self.__class__.__name__,
  199. self.args[0], self.args[1]))
  200. class InvalidFrameError(ProtocolSyntaxError):
  201. def __repr__(self):
  202. return '%s: Invalid frame received: %r' % (self.__class__.__name__,
  203. self.args[0])
  204. class InvalidFieldTypeException(ProtocolSyntaxError):
  205. def __repr__(self):
  206. return '%s: Unsupported field kind %s' % (self.__class__.__name__,
  207. self.args[0])
  208. class UnsupportedAMQPFieldException(ProtocolSyntaxError):
  209. def __repr__(self):
  210. return '%s: Unsupported field kind %s' % (self.__class__.__name__,
  211. type(self.args[1]))
  212. class MethodNotImplemented(AMQPError):
  213. pass
  214. class ChannelError(Exception):
  215. def __repr__(self):
  216. return '%s: An unspecified error occurred with the Channel' % (
  217. self.__class__.__name__)
  218. class ReentrancyError(Exception):
  219. """The requested operation would result in unsupported recursion or
  220. reentrancy.
  221. Used by BlockingConnection/BlockingChannel
  222. """
  223. class ShortStringTooLong(AMQPError):
  224. def __repr__(self):
  225. return ('%s: AMQP Short String can contain up to 255 bytes: '
  226. '%.300s' % (self.__class__.__name__, self.args[0]))
  227. class DuplicateGetOkCallback(ChannelError):
  228. def __repr__(self):
  229. return ('%s: basic_get can only be called again after the callback for '
  230. 'the previous basic_get is executed' % self.__class__.__name__)