Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. ###############################################################################
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (c) typedef int 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 autobahn.util import public
  27. from autobahn.wamp.uri import error
  28. __all__ = (
  29. 'Error',
  30. 'SessionNotReady',
  31. 'SerializationError',
  32. 'InvalidUriError',
  33. 'ProtocolError',
  34. 'TransportLost',
  35. 'ApplicationError',
  36. 'NotAuthorized',
  37. 'InvalidUri',
  38. 'InvalidPayload',
  39. 'TypeCheckError',
  40. )
  41. @public
  42. class Error(RuntimeError):
  43. """
  44. Base class for all exceptions related to WAMP.
  45. """
  46. @public
  47. class SessionNotReady(Error):
  48. """
  49. The application tried to perform a WAMP interaction, but the
  50. session is not yet fully established.
  51. """
  52. @public
  53. class SerializationError(Error):
  54. """
  55. Exception raised when the WAMP serializer could not serialize the
  56. application payload (``args`` or ``kwargs`` for ``CALL``, ``PUBLISH``, etc).
  57. """
  58. @public
  59. class InvalidUriError(Error):
  60. """
  61. Exception raised when an invalid WAMP URI was used.
  62. """
  63. @public
  64. class ProtocolError(Error):
  65. """
  66. Exception raised when WAMP protocol was violated. Protocol errors
  67. are fatal and are handled by the WAMP implementation. They are
  68. not supposed to be handled at the application level.
  69. """
  70. @public
  71. class TransportLost(Error):
  72. """
  73. Exception raised when the transport underlying the WAMP session
  74. was lost or is not connected.
  75. """
  76. @public
  77. class ApplicationError(Error):
  78. """
  79. Base class for all exceptions that can/may be handled
  80. at the application level.
  81. """
  82. INVALID_URI = "wamp.error.invalid_uri"
  83. """
  84. Peer provided an incorrect URI for a URI-based attribute of a WAMP message
  85. such as a realm, topic or procedure.
  86. """
  87. INVALID_PAYLOAD = "wamp.error.invalid_payload"
  88. """
  89. The application payload could not be serialized.
  90. """
  91. PAYLOAD_SIZE_EXCEEDED = "wamp.error.payload_size_exceeded"
  92. """
  93. The application payload could not be transported becuase the serialized/framed payload
  94. exceeds the transport limits.
  95. """
  96. NO_SUCH_PROCEDURE = "wamp.error.no_such_procedure"
  97. """
  98. A Dealer could not perform a call, since not procedure is currently registered
  99. under the given URI.
  100. """
  101. PROCEDURE_ALREADY_EXISTS = "wamp.error.procedure_already_exists"
  102. """
  103. A procedure could not be registered, since a procedure with the given URI is
  104. already registered.
  105. """
  106. PROCEDURE_EXISTS_INVOCATION_POLICY_CONFLICT = "wamp.error.procedure_exists_with_different_invocation_policy"
  107. """
  108. A procedure could not be registered, since a procedure with the given URI is
  109. already registered, and the registration has a conflicting invocation policy.
  110. """
  111. NO_SUCH_REGISTRATION = "wamp.error.no_such_registration"
  112. """
  113. A Dealer could not perform a unregister, since the given registration is not active.
  114. """
  115. NO_SUCH_SUBSCRIPTION = "wamp.error.no_such_subscription"
  116. """
  117. A Broker could not perform a unsubscribe, since the given subscription is not active.
  118. """
  119. NO_SUCH_SESSION = "wamp.error.no_such_session"
  120. """
  121. A router could not perform an operation, since a session ID specified was non-existant.
  122. """
  123. INVALID_ARGUMENT = "wamp.error.invalid_argument"
  124. """
  125. A call failed, since the given argument types or values are not acceptable to the
  126. called procedure - in which case the *Callee* may throw this error. Or a Router
  127. performing *payload validation* checked the payload (``args`` / ``kwargs``) of a call,
  128. call result, call error or publish, and the payload did not conform.
  129. """
  130. # FIXME: this currently isn't used neither in Autobahn nor Crossbar. Check!
  131. SYSTEM_SHUTDOWN = "wamp.error.system_shutdown"
  132. """
  133. The *Peer* is shutting down completely - used as a ``GOODBYE`` (or ``ABORT``) reason.
  134. """
  135. # FIXME: this currently isn't used neither in Autobahn nor Crossbar. Check!
  136. CLOSE_REALM = "wamp.error.close_realm"
  137. """
  138. The *Peer* want to leave the realm - used as a ``GOODBYE`` reason.
  139. """
  140. # FIXME: this currently isn't used neither in Autobahn nor Crossbar. Check!
  141. GOODBYE_AND_OUT = "wamp.error.goodbye_and_out"
  142. """
  143. A *Peer* acknowledges ending of a session - used as a ``GOOBYE`` reply reason.
  144. """
  145. NOT_AUTHORIZED = "wamp.error.not_authorized"
  146. """
  147. A call, register, publish or subscribe failed, since the session is not authorized
  148. to perform the operation.
  149. """
  150. AUTHORIZATION_FAILED = "wamp.error.authorization_failed"
  151. """
  152. A Dealer or Broker could not determine if the *Peer* is authorized to perform
  153. a join, call, register, publish or subscribe, since the authorization operation
  154. *itself* failed. E.g. a custom authorizer did run into an error.
  155. """
  156. AUTHENTICATION_FAILED = "wamp.error.authentication_failed"
  157. """
  158. Something failed with the authentication itself, that is, authentication could
  159. not run to end.
  160. """
  161. NO_AUTH_METHOD = "wamp.error.no_auth_method"
  162. """
  163. No authentication method the peer offered is available or active.
  164. """
  165. NO_SUCH_REALM = "wamp.error.no_such_realm"
  166. """
  167. Peer wanted to join a non-existing realm (and the *Router* did not allow to auto-create
  168. the realm).
  169. """
  170. NO_SUCH_ROLE = "wamp.error.no_such_role"
  171. """
  172. A *Peer* was to be authenticated under a Role that does not (or no longer) exists on the Router.
  173. For example, the *Peer* was successfully authenticated, but the Role configured does not
  174. exists - hence there is some misconfiguration in the Router.
  175. """
  176. NO_SUCH_PRINCIPAL = "wamp.error.no_such_principal"
  177. """
  178. A *Peer* was authenticated for an authid that does not or longer exists.
  179. """
  180. CANCELED = "wamp.error.canceled"
  181. """
  182. A Dealer or Callee canceled a call previously issued (WAMP AP).
  183. """
  184. TIMEOUT = "wamp.error.timeout"
  185. """
  186. A pending (in-flight) call was timed out.
  187. """
  188. # FIXME: this currently isn't used neither in Autobahn nor Crossbar. Check!
  189. NO_ELIGIBLE_CALLEE = "wamp.error.no_eligible_callee"
  190. """
  191. A *Dealer* could not perform a call, since a procedure with the given URI is registered,
  192. but *Callee Black- and Whitelisting* and/or *Caller Exclusion* lead to the
  193. exclusion of (any) *Callee* providing the procedure (WAMP AP).
  194. """
  195. ENC_NO_PAYLOAD_CODEC = "wamp.error.no_payload_codec"
  196. """
  197. WAMP message in payload transparency mode received, but no codec set
  198. or codec did not decode the payload.
  199. """
  200. ENC_TRUSTED_URI_MISMATCH = "wamp.error.encryption.trusted_uri_mismatch"
  201. """
  202. WAMP-cryptobox application payload end-to-end encryption error.
  203. """
  204. ENC_DECRYPT_ERROR = "wamp.error.encryption.decrypt_error"
  205. """
  206. WAMP-cryptobox application payload end-to-end encryption error.
  207. """
  208. TYPE_CHECK_ERROR = "wamp.error.type_check_error"
  209. """
  210. WAMP procedure called with wrong argument types or subscription published
  211. with wrong argument types.
  212. """
  213. def __init__(self, error, *args, **kwargs):
  214. """
  215. :param error: The URI of the error that occurred, e.g. ``wamp.error.not_authorized``.
  216. :type error: str
  217. """
  218. Exception.__init__(self, *args)
  219. self.kwargs = kwargs
  220. self.error = error
  221. self.enc_algo = kwargs.pop('enc_algo', None)
  222. self.callee = kwargs.pop('callee', None)
  223. self.callee_authid = kwargs.pop('callee_authid', None)
  224. self.callee_authrole = kwargs.pop('callee_authrole', None)
  225. self.forward_for = kwargs.pop('forward_for', None)
  226. @public
  227. def error_message(self):
  228. """
  229. Get the error message of this exception.
  230. :returns: The error message.
  231. :rtype: str
  232. """
  233. return '{0}: {1}'.format(
  234. self.error,
  235. ' '.join([str(a) for a in self.args]),
  236. )
  237. def __unicode__(self):
  238. if self.kwargs and 'traceback' in self.kwargs:
  239. tb = ':\n' + self.kwargs.pop('traceback') + '\n'
  240. self.kwargs['traceback'] = '...'
  241. else:
  242. tb = ''
  243. return "ApplicationError(error=<{0}>, args={1}, kwargs={2}, enc_algo={3}, callee={4}, callee_authid={5}, callee_authrole={6}, forward_for={7}){8}".format(
  244. self.error, list(self.args), self.kwargs, self.enc_algo, self.callee, self.callee_authid, self.callee_authrole, self.forward_for, tb)
  245. def __str__(self):
  246. return self.__unicode__()
  247. @error(ApplicationError.NOT_AUTHORIZED)
  248. class NotAuthorized(Exception):
  249. """
  250. Not authorized to perform the respective action.
  251. """
  252. @error(ApplicationError.INVALID_URI)
  253. class InvalidUri(Exception):
  254. """
  255. The URI for a topic, procedure or error is not a valid WAMP URI.
  256. """
  257. @error(ApplicationError.INVALID_PAYLOAD)
  258. class InvalidPayload(Exception):
  259. """
  260. The URI for a topic, procedure or error is not a valid WAMP URI.
  261. """
  262. class TypeCheckError(ApplicationError):
  263. """
  264. The URI for a topic published with invalid argument types or a
  265. procedure called with invalid arguments types.
  266. """
  267. def __init__(self, *args, **kwargs):
  268. super().__init__(ApplicationError.TYPE_CHECK_ERROR, *args, **kwargs)