2014-08-12 16:10:52 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-01-06 14:21:39 +01:00
|
|
|
# Copyright 2014, 2015 OpenMarket Ltd
|
2014-08-12 16:10:52 +02:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2014-08-13 04:14:34 +02:00
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
"""Contains exceptions and error codes."""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2014-11-20 18:10:37 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
class Codes(object):
|
2015-01-22 18:38:53 +01:00
|
|
|
UNRECOGNIZED = "M_UNRECOGNIZED"
|
2014-10-13 17:39:15 +02:00
|
|
|
UNAUTHORIZED = "M_UNAUTHORIZED"
|
2014-08-12 16:10:52 +02:00
|
|
|
FORBIDDEN = "M_FORBIDDEN"
|
|
|
|
BAD_JSON = "M_BAD_JSON"
|
|
|
|
NOT_JSON = "M_NOT_JSON"
|
|
|
|
USER_IN_USE = "M_USER_IN_USE"
|
|
|
|
ROOM_IN_USE = "M_ROOM_IN_USE"
|
|
|
|
BAD_PAGINATION = "M_BAD_PAGINATION"
|
|
|
|
UNKNOWN = "M_UNKNOWN"
|
|
|
|
NOT_FOUND = "M_NOT_FOUND"
|
2015-04-23 14:23:44 +02:00
|
|
|
MISSING_TOKEN = "M_MISSING_TOKEN"
|
2014-08-14 14:47:39 +02:00
|
|
|
UNKNOWN_TOKEN = "M_UNKNOWN_TOKEN"
|
2015-11-04 18:29:07 +01:00
|
|
|
GUEST_ACCESS_FORBIDDEN = "M_GUEST_ACCESS_FORBIDDEN"
|
2014-09-02 18:57:04 +02:00
|
|
|
LIMIT_EXCEEDED = "M_LIMIT_EXCEEDED"
|
2014-09-06 04:18:23 +02:00
|
|
|
CAPTCHA_NEEDED = "M_CAPTCHA_NEEDED"
|
|
|
|
CAPTCHA_INVALID = "M_CAPTCHA_INVALID"
|
2015-04-24 10:27:42 +02:00
|
|
|
MISSING_PARAM = "M_MISSING_PARAM"
|
|
|
|
TOO_LARGE = "M_TOO_LARGE"
|
2015-02-06 11:57:14 +01:00
|
|
|
EXCLUSIVE = "M_EXCLUSIVE"
|
2015-04-23 19:20:17 +02:00
|
|
|
THREEPID_AUTH_FAILED = "M_THREEPID_AUTH_FAILED"
|
2015-08-04 15:37:09 +02:00
|
|
|
THREEPID_IN_USE = "THREEPID_IN_USE"
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
|
2015-02-04 17:28:12 +01:00
|
|
|
class CodeMessageException(RuntimeError):
|
2014-08-12 16:10:52 +02:00
|
|
|
"""An exception with integer code and message string attributes."""
|
|
|
|
|
|
|
|
def __init__(self, code, msg):
|
|
|
|
super(CodeMessageException, self).__init__("%d: %s" % (code, msg))
|
|
|
|
self.code = code
|
|
|
|
self.msg = msg
|
2014-09-03 10:37:44 +02:00
|
|
|
self.response_code_message = None
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-09-03 09:58:48 +02:00
|
|
|
def error_dict(self):
|
|
|
|
return cs_error(self.msg)
|
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
class SynapseError(CodeMessageException):
|
|
|
|
"""A base error which can be caught for all synapse events."""
|
2014-09-03 09:58:48 +02:00
|
|
|
def __init__(self, code, msg, errcode=Codes.UNKNOWN):
|
2014-08-12 16:10:52 +02:00
|
|
|
"""Constructs a synapse error.
|
|
|
|
|
|
|
|
Args:
|
2014-10-30 12:10:17 +01:00
|
|
|
code (int): The integer error code (an HTTP response code)
|
2014-08-12 16:10:52 +02:00
|
|
|
msg (str): The human-readable error message.
|
|
|
|
err (str): The error code e.g 'M_FORBIDDEN'
|
|
|
|
"""
|
|
|
|
super(SynapseError, self).__init__(code, msg)
|
|
|
|
self.errcode = errcode
|
|
|
|
|
2014-09-03 09:58:48 +02:00
|
|
|
def error_dict(self):
|
|
|
|
return cs_error(
|
|
|
|
self.msg,
|
|
|
|
self.errcode,
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
2014-10-30 12:10:17 +01:00
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
class RegistrationError(SynapseError):
|
|
|
|
"""An error raised when a registration event fails."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-01-22 18:38:53 +01:00
|
|
|
class UnrecognizedRequestError(SynapseError):
|
|
|
|
"""An error indicating we don't understand the request you're trying to make"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
if "errcode" not in kwargs:
|
2015-01-22 20:32:17 +01:00
|
|
|
kwargs["errcode"] = Codes.UNRECOGNIZED
|
2015-01-23 12:19:02 +01:00
|
|
|
message = None
|
|
|
|
if len(args) == 0:
|
|
|
|
message = "Unrecognized request"
|
|
|
|
else:
|
|
|
|
message = args[0]
|
2015-01-22 18:38:53 +01:00
|
|
|
super(UnrecognizedRequestError, self).__init__(
|
|
|
|
400,
|
2015-01-23 12:19:02 +01:00
|
|
|
message,
|
2015-01-22 18:38:53 +01:00
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
2015-01-22 20:32:17 +01:00
|
|
|
|
|
|
|
class NotFoundError(SynapseError):
|
|
|
|
"""An error indicating we can't find the thing you asked for"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
if "errcode" not in kwargs:
|
|
|
|
kwargs["errcode"] = Codes.NOT_FOUND
|
2015-01-23 11:32:40 +01:00
|
|
|
super(NotFoundError, self).__init__(
|
2015-01-22 20:32:17 +01:00
|
|
|
404,
|
|
|
|
"Not found",
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
2015-01-29 17:10:35 +01:00
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
class AuthError(SynapseError):
|
|
|
|
"""An error raised when there was a problem authorising an event."""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
if "errcode" not in kwargs:
|
|
|
|
kwargs["errcode"] = Codes.FORBIDDEN
|
|
|
|
super(AuthError, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2015-10-22 12:44:31 +02:00
|
|
|
class EventSizeError(SynapseError):
|
|
|
|
"""An error raised when an event is too big."""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
if "errcode" not in kwargs:
|
|
|
|
kwargs["errcode"] = Codes.TOO_LARGE
|
|
|
|
super(EventSizeError, self).__init__(413, *args, **kwargs)
|
|
|
|
|
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
class EventStreamError(SynapseError):
|
|
|
|
"""An error raised when there a problem with the event stream."""
|
2014-08-14 12:57:25 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
if "errcode" not in kwargs:
|
|
|
|
kwargs["errcode"] = Codes.BAD_PAGINATION
|
|
|
|
super(EventStreamError, self).__init__(*args, **kwargs)
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
class LoginError(SynapseError):
|
|
|
|
"""An error raised when there was a problem logging in."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class StoreError(SynapseError):
|
|
|
|
"""An error raised when there was a problem storing some data."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-09-06 04:18:23 +02:00
|
|
|
class InvalidCaptchaError(SynapseError):
|
|
|
|
def __init__(self, code=400, msg="Invalid captcha.", error_url=None,
|
|
|
|
errcode=Codes.CAPTCHA_INVALID):
|
|
|
|
super(InvalidCaptchaError, self).__init__(code, msg, errcode)
|
|
|
|
self.error_url = error_url
|
|
|
|
|
|
|
|
def error_dict(self):
|
|
|
|
return cs_error(
|
|
|
|
self.msg,
|
|
|
|
self.errcode,
|
|
|
|
error_url=self.error_url,
|
|
|
|
)
|
|
|
|
|
2014-10-30 12:10:17 +01:00
|
|
|
|
2014-09-03 09:58:48 +02:00
|
|
|
class LimitExceededError(SynapseError):
|
|
|
|
"""A client has sent too many requests and is being throttled.
|
|
|
|
"""
|
|
|
|
def __init__(self, code=429, msg="Too Many Requests", retry_after_ms=None,
|
|
|
|
errcode=Codes.LIMIT_EXCEEDED):
|
|
|
|
super(LimitExceededError, self).__init__(code, msg, errcode)
|
|
|
|
self.retry_after_ms = retry_after_ms
|
2014-09-03 10:37:44 +02:00
|
|
|
self.response_code_message = "Too Many Requests"
|
2014-09-03 09:58:48 +02:00
|
|
|
|
|
|
|
def error_dict(self):
|
2014-08-12 16:10:52 +02:00
|
|
|
return cs_error(
|
2014-09-03 09:58:48 +02:00
|
|
|
self.msg,
|
|
|
|
self.errcode,
|
|
|
|
retry_after_ms=self.retry_after_ms,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def cs_exception(exception):
|
|
|
|
if isinstance(exception, CodeMessageException):
|
|
|
|
return exception.error_dict()
|
2014-08-12 16:10:52 +02:00
|
|
|
else:
|
2014-11-20 18:10:37 +01:00
|
|
|
logger.error("Unknown exception type: %s", type(exception))
|
|
|
|
return {}
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
def cs_error(msg, code=Codes.UNKNOWN, **kwargs):
|
|
|
|
""" Utility method for constructing an error response for client-server
|
|
|
|
interactions.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
msg (str): The error message.
|
|
|
|
code (int): The error code.
|
|
|
|
kwargs : Additional keys to add to the response.
|
|
|
|
Returns:
|
|
|
|
A dict representing the error response JSON.
|
|
|
|
"""
|
|
|
|
err = {"error": msg, "errcode": code}
|
|
|
|
for key, value in kwargs.iteritems():
|
|
|
|
err[key] = value
|
|
|
|
return err
|
2014-11-04 16:10:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
class FederationError(RuntimeError):
|
|
|
|
""" This class is used to inform remote home servers about erroneous
|
|
|
|
PDUs they sent us.
|
|
|
|
|
|
|
|
FATAL: The remote server could not interpret the source event.
|
|
|
|
(e.g., it was missing a required field)
|
|
|
|
ERROR: The remote server interpreted the event, but it failed some other
|
|
|
|
check (e.g. auth)
|
|
|
|
WARN: The remote server accepted the event, but believes some part of it
|
|
|
|
is wrong (e.g., it referred to an invalid event)
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, level, code, reason, affected, source=None):
|
|
|
|
if level not in ["FATAL", "ERROR", "WARN"]:
|
|
|
|
raise ValueError("Level is not valid: %s" % (level,))
|
|
|
|
self.level = level
|
|
|
|
self.code = code
|
|
|
|
self.reason = reason
|
|
|
|
self.affected = affected
|
|
|
|
self.source = source
|
|
|
|
|
|
|
|
msg = "%s %s: %s" % (level, code, reason,)
|
|
|
|
super(FederationError, self).__init__(msg)
|
|
|
|
|
|
|
|
def get_dict(self):
|
|
|
|
return {
|
|
|
|
"level": self.level,
|
|
|
|
"code": self.code,
|
|
|
|
"reason": self.reason,
|
|
|
|
"affected": self.affected,
|
|
|
|
"source": self.source if self.source else self.affected,
|
|
|
|
}
|
2015-02-04 17:28:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
class HttpResponseException(CodeMessageException):
|
|
|
|
def __init__(self, code, msg, response):
|
|
|
|
self.response = response
|
|
|
|
super(HttpResponseException, self).__init__(code, msg)
|