Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

exceptions.py 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. """Exceptions used by amqp"""
  2. # Copyright (C) 2007-2008 Barry Pederson <bp@barryp.org>
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Lesser General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2.1 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public
  15. # License along with this library; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
  17. from __future__ import absolute_import
  18. from struct import pack, unpack
  19. __all__ = [
  20. 'AMQPError',
  21. 'ConnectionError', 'ChannelError',
  22. 'RecoverableConnectionError', 'IrrecoverableConnectionError',
  23. 'RecoverableChannelError', 'IrrecoverableChannelError',
  24. 'ConsumerCancelled', 'ContentTooLarge', 'NoConsumers',
  25. 'ConnectionForced', 'InvalidPath', 'AccessRefused', 'NotFound',
  26. 'ResourceLocked', 'PreconditionFailed', 'FrameError', 'FrameSyntaxError',
  27. 'InvalidCommand', 'ChannelNotOpen', 'UnexpectedFrame', 'ResourceError',
  28. 'NotConfirmed', 'NotAllowed', 'AMQPNotImplementedError', 'InternalError',
  29. ]
  30. class AMQPError(Exception):
  31. code = 0
  32. def __init__(self, reply_text=None, method_sig=None,
  33. method_name=None, reply_code=None):
  34. self.message = reply_text
  35. self.reply_code = reply_code or self.code
  36. self.reply_text = reply_text
  37. self.method_sig = method_sig
  38. self.method_name = method_name or ''
  39. if method_sig and not self.method_name:
  40. self.method_name = METHOD_NAME_MAP.get(method_sig, '')
  41. Exception.__init__(self, reply_code,
  42. reply_text, method_sig, self.method_name)
  43. def __str__(self):
  44. if self.method:
  45. return '{0.method}: ({0.reply_code}) {0.reply_text}'.format(self)
  46. return self.reply_text or '<AMQPError: unknown error>'
  47. @property
  48. def method(self):
  49. return self.method_name or self.method_sig
  50. class ConnectionError(AMQPError):
  51. pass
  52. class ChannelError(AMQPError):
  53. pass
  54. class RecoverableChannelError(ChannelError):
  55. pass
  56. class IrrecoverableChannelError(ChannelError):
  57. pass
  58. class RecoverableConnectionError(ConnectionError):
  59. pass
  60. class IrrecoverableConnectionError(ConnectionError):
  61. pass
  62. class Blocked(RecoverableConnectionError):
  63. pass
  64. class ConsumerCancelled(RecoverableConnectionError):
  65. pass
  66. class ContentTooLarge(RecoverableChannelError):
  67. code = 311
  68. class NoConsumers(RecoverableChannelError):
  69. code = 313
  70. class ConnectionForced(RecoverableConnectionError):
  71. code = 320
  72. class InvalidPath(IrrecoverableConnectionError):
  73. code = 402
  74. class AccessRefused(IrrecoverableChannelError):
  75. code = 403
  76. class NotFound(IrrecoverableChannelError):
  77. code = 404
  78. class NotConfirmed(RecoverableConnectionError):
  79. pass
  80. class ResourceLocked(RecoverableChannelError):
  81. code = 405
  82. class PreconditionFailed(IrrecoverableChannelError):
  83. code = 406
  84. class FrameError(IrrecoverableConnectionError):
  85. code = 501
  86. class FrameSyntaxError(IrrecoverableConnectionError):
  87. code = 502
  88. class InvalidCommand(IrrecoverableConnectionError):
  89. code = 503
  90. class ChannelNotOpen(IrrecoverableConnectionError):
  91. code = 504
  92. class UnexpectedFrame(IrrecoverableConnectionError):
  93. code = 505
  94. class ResourceError(RecoverableConnectionError):
  95. code = 506
  96. class NotAllowed(IrrecoverableConnectionError):
  97. code = 530
  98. class AMQPNotImplementedError(IrrecoverableConnectionError):
  99. code = 540
  100. class InternalError(IrrecoverableConnectionError):
  101. code = 541
  102. ERROR_MAP = {
  103. 311: ContentTooLarge,
  104. 313: NoConsumers,
  105. 320: ConnectionForced,
  106. 402: InvalidPath,
  107. 403: AccessRefused,
  108. 404: NotFound,
  109. 405: ResourceLocked,
  110. 406: PreconditionFailed,
  111. 501: FrameError,
  112. 502: FrameSyntaxError,
  113. 503: InvalidCommand,
  114. 504: ChannelNotOpen,
  115. 505: UnexpectedFrame,
  116. 506: ResourceError,
  117. 530: NotAllowed,
  118. 540: AMQPNotImplementedError,
  119. 541: InternalError,
  120. }
  121. def error_for_code(code, text, method, default):
  122. try:
  123. return ERROR_MAP[code](text, method, reply_code=code)
  124. except KeyError:
  125. return default(text, method, reply_code=code)
  126. def raise_for_code(code, text, method, default):
  127. raise error_for_code(code, text, method, default)
  128. METHOD_NAME_MAP = {
  129. (10, 10): 'Connection.start',
  130. (10, 11): 'Connection.start_ok',
  131. (10, 20): 'Connection.secure',
  132. (10, 21): 'Connection.secure_ok',
  133. (10, 30): 'Connection.tune',
  134. (10, 31): 'Connection.tune_ok',
  135. (10, 40): 'Connection.open',
  136. (10, 41): 'Connection.open_ok',
  137. (10, 50): 'Connection.close',
  138. (10, 51): 'Connection.close_ok',
  139. (20, 10): 'Channel.open',
  140. (20, 11): 'Channel.open_ok',
  141. (20, 20): 'Channel.flow',
  142. (20, 21): 'Channel.flow_ok',
  143. (20, 40): 'Channel.close',
  144. (20, 41): 'Channel.close_ok',
  145. (30, 10): 'Access.request',
  146. (30, 11): 'Access.request_ok',
  147. (40, 10): 'Exchange.declare',
  148. (40, 11): 'Exchange.declare_ok',
  149. (40, 20): 'Exchange.delete',
  150. (40, 21): 'Exchange.delete_ok',
  151. (40, 30): 'Exchange.bind',
  152. (40, 31): 'Exchange.bind_ok',
  153. (40, 40): 'Exchange.unbind',
  154. (40, 41): 'Exchange.unbind_ok',
  155. (50, 10): 'Queue.declare',
  156. (50, 11): 'Queue.declare_ok',
  157. (50, 20): 'Queue.bind',
  158. (50, 21): 'Queue.bind_ok',
  159. (50, 30): 'Queue.purge',
  160. (50, 31): 'Queue.purge_ok',
  161. (50, 40): 'Queue.delete',
  162. (50, 41): 'Queue.delete_ok',
  163. (50, 50): 'Queue.unbind',
  164. (50, 51): 'Queue.unbind_ok',
  165. (60, 10): 'Basic.qos',
  166. (60, 11): 'Basic.qos_ok',
  167. (60, 20): 'Basic.consume',
  168. (60, 21): 'Basic.consume_ok',
  169. (60, 30): 'Basic.cancel',
  170. (60, 31): 'Basic.cancel_ok',
  171. (60, 40): 'Basic.publish',
  172. (60, 50): 'Basic.return',
  173. (60, 60): 'Basic.deliver',
  174. (60, 70): 'Basic.get',
  175. (60, 71): 'Basic.get_ok',
  176. (60, 72): 'Basic.get_empty',
  177. (60, 80): 'Basic.ack',
  178. (60, 90): 'Basic.reject',
  179. (60, 100): 'Basic.recover_async',
  180. (60, 110): 'Basic.recover',
  181. (60, 111): 'Basic.recover_ok',
  182. (60, 120): 'Basic.nack',
  183. (90, 10): 'Tx.select',
  184. (90, 11): 'Tx.select_ok',
  185. (90, 20): 'Tx.commit',
  186. (90, 21): 'Tx.commit_ok',
  187. (90, 30): 'Tx.rollback',
  188. (90, 31): 'Tx.rollback_ok',
  189. (85, 10): 'Confirm.select',
  190. (85, 11): 'Confirm.select_ok',
  191. }
  192. for _method_id, _method_name in list(METHOD_NAME_MAP.items()):
  193. METHOD_NAME_MAP[unpack('>I', pack('>HH', *_method_id))[0]] = _method_name