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.

exception.py 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. ###############################################################################
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (c) Crossbar.io Technologies GmbH
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in
  15. # all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. # THE SOFTWARE.
  24. #
  25. ###############################################################################
  26. from __future__ import absolute_import
  27. import six
  28. from autobahn.util import public
  29. from autobahn.wamp.uri import error
  30. __all__ = (
  31. 'Error',
  32. 'SessionNotReady',
  33. 'SerializationError',
  34. 'ProtocolError',
  35. 'TransportLost',
  36. 'ApplicationError',
  37. 'NotAuthorized',
  38. 'InvalidUri',
  39. )
  40. @public
  41. class Error(RuntimeError):
  42. """
  43. Base class for all exceptions related to WAMP.
  44. """
  45. @public
  46. class SessionNotReady(Error):
  47. """
  48. The application tried to perform a WAMP interaction, but the
  49. session is not yet fully established.
  50. """
  51. @public
  52. class SerializationError(Error):
  53. """
  54. Exception raised when the WAMP serializer could not serialize the
  55. application payload (``args`` or ``kwargs`` for ``CALL``, ``PUBLISH``, etc).
  56. """
  57. @public
  58. class InvalidUriError(Error):
  59. """
  60. Exception raised when an invalid WAMP URI was used.
  61. """
  62. @public
  63. class ProtocolError(Error):
  64. """
  65. Exception raised when WAMP protocol was violated. Protocol errors
  66. are fatal and are handled by the WAMP implementation. They are
  67. not supposed to be handled at the application level.
  68. """
  69. @public
  70. class TransportLost(Error):
  71. """
  72. Exception raised when the transport underlying the WAMP session
  73. was lost or is not connected.
  74. """
  75. @public
  76. class ApplicationError(Error):
  77. """
  78. Base class for all exceptions that can/may be handled
  79. at the application level.
  80. """
  81. INVALID_URI = u"wamp.error.invalid_uri"
  82. """
  83. Peer provided an incorrect URI for a URI-based attribute of a WAMP message
  84. such as a realm, topic or procedure.
  85. """
  86. INVALID_PAYLOAD = u"wamp.error.invalid_payload"
  87. """
  88. The application payload could not be serialized.
  89. """
  90. PAYLOAD_SIZE_EXCEEDED = u"wamp.error.payload_size_exceeded"
  91. """
  92. The application payload could not be transported becuase the serialized/framed payload
  93. exceeds the transport limits.
  94. """
  95. NO_SUCH_PROCEDURE = u"wamp.error.no_such_procedure"
  96. """
  97. A Dealer could not perform a call, since not procedure is currently registered
  98. under the given URI.
  99. """
  100. PROCEDURE_ALREADY_EXISTS = u"wamp.error.procedure_already_exists"
  101. """
  102. A procedure could not be registered, since a procedure with the given URI is
  103. already registered.
  104. """
  105. PROCEDURE_EXISTS_INVOCATION_POLICY_CONFLICT = u"wamp.error.procedure_exists_with_different_invocation_policy"
  106. """
  107. A procedure could not be registered, since a procedure with the given URI is
  108. already registered, and the registration has a conflicting invocation policy.
  109. """
  110. NO_SUCH_REGISTRATION = u"wamp.error.no_such_registration"
  111. """
  112. A Dealer could not perform a unregister, since the given registration is not active.
  113. """
  114. NO_SUCH_SUBSCRIPTION = u"wamp.error.no_such_subscription"
  115. """
  116. A Broker could not perform a unsubscribe, since the given subscription is not active.
  117. """
  118. NO_SUCH_SESSION = u"wamp.error.no_such_session"
  119. """
  120. A router could not perform an operation, since a session ID specified was non-existant.
  121. """
  122. INVALID_ARGUMENT = u"wamp.error.invalid_argument"
  123. """
  124. A call failed, since the given argument types or values are not acceptable to the
  125. called procedure - in which case the *Callee* may throw this error. Or a Router
  126. performing *payload validation* checked the payload (``args`` / ``kwargs``) of a call,
  127. call result, call error or publish, and the payload did not conform.
  128. """
  129. # FIXME: this currently isn't used neither in Autobahn nor Crossbar. Check!
  130. SYSTEM_SHUTDOWN = u"wamp.error.system_shutdown"
  131. """
  132. The *Peer* is shutting down completely - used as a ``GOODBYE`` (or ``ABORT``) reason.
  133. """
  134. # FIXME: this currently isn't used neither in Autobahn nor Crossbar. Check!
  135. CLOSE_REALM = u"wamp.error.close_realm"
  136. """
  137. The *Peer* want to leave the realm - used as a ``GOODBYE`` reason.
  138. """
  139. # FIXME: this currently isn't used neither in Autobahn nor Crossbar. Check!
  140. GOODBYE_AND_OUT = u"wamp.error.goodbye_and_out"
  141. """
  142. A *Peer* acknowledges ending of a session - used as a ``GOOBYE`` reply reason.
  143. """
  144. NOT_AUTHORIZED = u"wamp.error.not_authorized"
  145. """
  146. A call, register, publish or subscribe failed, since the session is not authorized
  147. to perform the operation.
  148. """
  149. AUTHORIZATION_FAILED = u"wamp.error.authorization_failed"
  150. """
  151. A Dealer or Broker could not determine if the *Peer* is authorized to perform
  152. a join, call, register, publish or subscribe, since the authorization operation
  153. *itself* failed. E.g. a custom authorizer did run into an error.
  154. """
  155. AUTHENTICATION_FAILED = u"wamp.error.authentication_failed"
  156. """
  157. Something failed with the authentication itself, that is, authentication could
  158. not run to end.
  159. """
  160. NO_AUTH_METHOD = u"wamp.error.no_auth_method"
  161. """
  162. No authentication method the peer offered is available or active.
  163. """
  164. NO_SUCH_REALM = u"wamp.error.no_such_realm"
  165. """
  166. Peer wanted to join a non-existing realm (and the *Router* did not allow to auto-create
  167. the realm).
  168. """
  169. NO_SUCH_ROLE = u"wamp.error.no_such_role"
  170. """
  171. A *Peer* was to be authenticated under a Role that does not (or no longer) exists on the Router.
  172. For example, the *Peer* was successfully authenticated, but the Role configured does not
  173. exists - hence there is some misconfiguration in the Router.
  174. """
  175. NO_SUCH_PRINCIPAL = u"wamp.error.no_such_principal"
  176. """
  177. A *Peer* was authenticated for an authid that does not or longer exists.
  178. """
  179. CANCELED = u"wamp.error.canceled"
  180. """
  181. A Dealer or Callee canceled a call previously issued (WAMP AP).
  182. """
  183. TIMEOUT = u"wamp.error.timeout"
  184. """
  185. A pending (in-flight) call was timed out.
  186. """
  187. # FIXME: this currently isn't used neither in Autobahn nor Crossbar. Check!
  188. NO_ELIGIBLE_CALLEE = u"wamp.error.no_eligible_callee"
  189. """
  190. A *Dealer* could not perform a call, since a procedure with the given URI is registered,
  191. but *Callee Black- and Whitelisting* and/or *Caller Exclusion* lead to the
  192. exclusion of (any) *Callee* providing the procedure (WAMP AP).
  193. """
  194. ENC_NO_PAYLOAD_CODEC = u"wamp.error.no_payload_codec"
  195. """
  196. WAMP message in payload transparency mode received, but no codec set
  197. or codec did not decode the payload.
  198. """
  199. ENC_TRUSTED_URI_MISMATCH = u"wamp.error.encryption.trusted_uri_mismatch"
  200. """
  201. WAMP-cryptobox application payload end-to-end encryption error.
  202. """
  203. ENC_DECRYPT_ERROR = u"wamp.error.encryption.decrypt_error"
  204. """
  205. WAMP-cryptobox application payload end-to-end encryption error.
  206. """
  207. def __init__(self, error, *args, **kwargs):
  208. """
  209. :param error: The URI of the error that occurred, e.g. ``wamp.error.not_authorized``.
  210. :type error: str
  211. """
  212. Exception.__init__(self, *args)
  213. self.kwargs = kwargs
  214. self.error = error
  215. self.enc_algo = kwargs.pop('enc_algo', None)
  216. self.callee = kwargs.pop('callee', None)
  217. self.callee_authid = kwargs.pop('callee_authid', None)
  218. self.callee_authrole = kwargs.pop('callee_authrole', None)
  219. self.forward_for = kwargs.pop('forward_for', None)
  220. @public
  221. def error_message(self):
  222. """
  223. Get the error message of this exception.
  224. :returns: The error message.
  225. :rtype: str
  226. """
  227. return u'{0}: {1}'.format(
  228. self.error,
  229. u' '.join([six.text_type(a) for a in self.args]),
  230. )
  231. def __unicode__(self):
  232. if self.kwargs and 'traceback' in self.kwargs:
  233. tb = u':\n' + u'\n'.join(self.kwargs.pop('traceback')) + u'\n'
  234. self.kwargs['traceback'] = u'...'
  235. else:
  236. tb = u''
  237. return u"ApplicationError(error=<{0}>, args={1}, kwargs={2}, enc_algo={3}, callee={4}, callee_authid={5}, callee_authrole={6}, forward_for={7}){8}".format(
  238. self.error, list(self.args), self.kwargs, self.enc_algo, self.callee, self.callee_authid, self.callee_authrole, self.forward_for, tb)
  239. def __str__(self):
  240. if six.PY3:
  241. return self.__unicode__()
  242. else:
  243. return self.__unicode__().encode('utf8')
  244. @error(ApplicationError.NOT_AUTHORIZED)
  245. class NotAuthorized(Exception):
  246. """
  247. Not authorized to perform the respective action.
  248. """
  249. @error(ApplicationError.INVALID_URI)
  250. class InvalidUri(Exception):
  251. """
  252. The URI for a topic, procedure or error is not a valid WAMP URI.
  253. """
  254. @error(ApplicationError.INVALID_PAYLOAD)
  255. class InvalidPayload(Exception):
  256. """
  257. The URI for a topic, procedure or error is not a valid WAMP URI.
  258. """