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.
 
 
 
 
 
 

115 lines
3.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 matrix.org
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Contains exceptions and error codes."""
  16. import logging
  17. class Codes(object):
  18. FORBIDDEN = "M_FORBIDDEN"
  19. BAD_JSON = "M_BAD_JSON"
  20. NOT_JSON = "M_NOT_JSON"
  21. USER_IN_USE = "M_USER_IN_USE"
  22. ROOM_IN_USE = "M_ROOM_IN_USE"
  23. BAD_PAGINATION = "M_BAD_PAGINATION"
  24. UNKNOWN = "M_UNKNOWN"
  25. NOT_FOUND = "M_NOT_FOUND"
  26. class CodeMessageException(Exception):
  27. """An exception with integer code and message string attributes."""
  28. def __init__(self, code, msg):
  29. logging.error("%s: %s, %s", type(self).__name__, code, msg)
  30. super(CodeMessageException, self).__init__("%d: %s" % (code, msg))
  31. self.code = code
  32. self.msg = msg
  33. class SynapseError(CodeMessageException):
  34. """A base error which can be caught for all synapse events."""
  35. def __init__(self, code, msg, errcode=""):
  36. """Constructs a synapse error.
  37. Args:
  38. code (int): The integer error code (typically an HTTP response code)
  39. msg (str): The human-readable error message.
  40. err (str): The error code e.g 'M_FORBIDDEN'
  41. """
  42. super(SynapseError, self).__init__(code, msg)
  43. self.errcode = errcode
  44. class RoomError(SynapseError):
  45. """An error raised when a room event fails."""
  46. pass
  47. class RegistrationError(SynapseError):
  48. """An error raised when a registration event fails."""
  49. pass
  50. class AuthError(SynapseError):
  51. """An error raised when there was a problem authorising an event."""
  52. def __init__(self, *args, **kwargs):
  53. if "errcode" not in kwargs:
  54. kwargs["errcode"] = Codes.FORBIDDEN
  55. super(AuthError, self).__init__(*args, **kwargs)
  56. class EventStreamError(SynapseError):
  57. """An error raised when there a problem with the event stream."""
  58. pass
  59. class LoginError(SynapseError):
  60. """An error raised when there was a problem logging in."""
  61. pass
  62. class StoreError(SynapseError):
  63. """An error raised when there was a problem storing some data."""
  64. pass
  65. def cs_exception(exception):
  66. if isinstance(exception, SynapseError):
  67. return cs_error(
  68. exception.msg,
  69. Codes.UNKNOWN if not exception.errcode else exception.errcode)
  70. elif isinstance(exception, CodeMessageException):
  71. return cs_error(exception.msg)
  72. else:
  73. logging.error("Unknown exception type: %s", type(exception))
  74. def cs_error(msg, code=Codes.UNKNOWN, **kwargs):
  75. """ Utility method for constructing an error response for client-server
  76. interactions.
  77. Args:
  78. msg (str): The error message.
  79. code (int): The error code.
  80. kwargs : Additional keys to add to the response.
  81. Returns:
  82. A dict representing the error response JSON.
  83. """
  84. err = {"error": msg, "errcode": code}
  85. for key, value in kwargs.iteritems():
  86. err[key] = value
  87. return err