2014-08-12 10:10:52 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 23:26:29 -05:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2018-07-25 17:10:39 -04:00
|
|
|
# Copyright 2018 New Vector Ltd.
|
2014-08-12 10:10:52 -04: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-12 22:14:34 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
"""Contains exceptions and error codes."""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2018-04-15 10:51:07 -04:00
|
|
|
from six import iteritems
|
2018-05-22 03:56:52 -04:00
|
|
|
from six.moves import http_client
|
2018-03-29 17:45:52 -04:00
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
from canonicaljson import json
|
|
|
|
|
2014-11-20 12:10:37 -05:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
class Codes(object):
|
2015-01-22 12:38:53 -05:00
|
|
|
UNRECOGNIZED = "M_UNRECOGNIZED"
|
2014-10-13 11:39:15 -04:00
|
|
|
UNAUTHORIZED = "M_UNAUTHORIZED"
|
2014-08-12 10:10:52 -04: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"
|
2016-01-15 11:27:26 -05:00
|
|
|
BAD_STATE = "M_BAD_STATE"
|
2014-08-12 10:10:52 -04:00
|
|
|
UNKNOWN = "M_UNKNOWN"
|
|
|
|
NOT_FOUND = "M_NOT_FOUND"
|
2015-04-23 08:23:44 -04:00
|
|
|
MISSING_TOKEN = "M_MISSING_TOKEN"
|
2014-08-14 08:47:39 -04:00
|
|
|
UNKNOWN_TOKEN = "M_UNKNOWN_TOKEN"
|
2015-11-04 12:29:07 -05:00
|
|
|
GUEST_ACCESS_FORBIDDEN = "M_GUEST_ACCESS_FORBIDDEN"
|
2014-09-02 12:57:04 -04:00
|
|
|
LIMIT_EXCEEDED = "M_LIMIT_EXCEEDED"
|
2014-09-05 22:18:23 -04:00
|
|
|
CAPTCHA_NEEDED = "M_CAPTCHA_NEEDED"
|
|
|
|
CAPTCHA_INVALID = "M_CAPTCHA_INVALID"
|
2015-04-24 04:27:42 -04:00
|
|
|
MISSING_PARAM = "M_MISSING_PARAM"
|
2016-11-21 08:13:55 -05:00
|
|
|
INVALID_PARAM = "M_INVALID_PARAM"
|
2015-04-24 04:27:42 -04:00
|
|
|
TOO_LARGE = "M_TOO_LARGE"
|
2015-02-06 05:57:14 -05:00
|
|
|
EXCLUSIVE = "M_EXCLUSIVE"
|
2015-04-23 13:20:17 -04:00
|
|
|
THREEPID_AUTH_FAILED = "M_THREEPID_AUTH_FAILED"
|
2016-06-30 12:51:28 -04:00
|
|
|
THREEPID_IN_USE = "M_THREEPID_IN_USE"
|
2016-07-08 12:42:48 -04:00
|
|
|
THREEPID_NOT_FOUND = "M_THREEPID_NOT_FOUND"
|
2018-01-18 19:19:58 -05:00
|
|
|
THREEPID_DENIED = "M_THREEPID_DENIED"
|
2016-01-15 05:06:34 -05:00
|
|
|
INVALID_USERNAME = "M_INVALID_USERNAME"
|
2016-06-30 12:51:28 -04:00
|
|
|
SERVER_NOT_TRUSTED = "M_SERVER_NOT_TRUSTED"
|
2018-05-22 03:56:52 -04:00
|
|
|
CONSENT_NOT_GIVEN = "M_CONSENT_NOT_GIVEN"
|
2018-05-22 12:27:03 -04:00
|
|
|
CANNOT_LEAVE_SERVER_NOTICE_ROOM = "M_CANNOT_LEAVE_SERVER_NOTICE_ROOM"
|
2018-08-18 09:39:45 -04:00
|
|
|
RESOURCE_LIMIT_EXCEEDED = "M_RESOURCE_LIMIT_EXCEEDED"
|
2018-07-25 17:10:39 -04:00
|
|
|
UNSUPPORTED_ROOM_VERSION = "M_UNSUPPORTED_ROOM_VERSION"
|
2018-07-25 17:25:41 -04:00
|
|
|
INCOMPATIBLE_ROOM_VERSION = "M_INCOMPATIBLE_ROOM_VERSION"
|
2017-12-05 20:02:57 -05:00
|
|
|
WRONG_ROOM_KEYS_VERSION = "M_WRONG_ROOM_KEYS_VERSION"
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
|
2015-02-04 11:28:12 -05:00
|
|
|
class CodeMessageException(RuntimeError):
|
2017-03-13 09:50:16 -04:00
|
|
|
"""An exception with integer code and message string attributes.
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2017-03-13 09:50:16 -04:00
|
|
|
Attributes:
|
|
|
|
code (int): HTTP error code
|
2017-03-14 08:36:50 -04:00
|
|
|
msg (str): string describing the error
|
2017-03-13 09:50:16 -04:00
|
|
|
"""
|
2017-03-14 08:36:50 -04:00
|
|
|
def __init__(self, code, msg):
|
|
|
|
super(CodeMessageException, self).__init__("%d: %s" % (code, msg))
|
2014-08-12 10:10:52 -04:00
|
|
|
self.code = code
|
2017-03-14 08:36:50 -04:00
|
|
|
self.msg = msg
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2017-04-25 14:30:55 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
class SynapseError(CodeMessageException):
|
2017-03-14 08:36:50 -04:00
|
|
|
"""A base exception type for matrix errors which have an errcode and error
|
|
|
|
message (as well as an HTTP status code).
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
errcode (str): Matrix error code e.g 'M_FORBIDDEN'
|
|
|
|
"""
|
2014-09-03 03:58:48 -04:00
|
|
|
def __init__(self, code, msg, errcode=Codes.UNKNOWN):
|
2014-08-12 10:10:52 -04:00
|
|
|
"""Constructs a synapse error.
|
|
|
|
|
|
|
|
Args:
|
2014-10-30 07:10:17 -04:00
|
|
|
code (int): The integer error code (an HTTP response code)
|
2014-08-12 10:10:52 -04:00
|
|
|
msg (str): The human-readable error message.
|
2017-03-14 08:36:50 -04:00
|
|
|
errcode (str): The matrix error code e.g 'M_FORBIDDEN'
|
2014-08-12 10:10:52 -04:00
|
|
|
"""
|
2017-03-14 08:36:50 -04:00
|
|
|
super(SynapseError, self).__init__(code, msg)
|
2014-08-12 10:10:52 -04:00
|
|
|
self.errcode = errcode
|
|
|
|
|
2014-09-03 03:58:48 -04:00
|
|
|
def error_dict(self):
|
|
|
|
return cs_error(
|
|
|
|
self.msg,
|
|
|
|
self.errcode,
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2017-03-14 09:36:06 -04:00
|
|
|
|
2018-08-01 09:58:16 -04:00
|
|
|
class ProxiedRequestError(SynapseError):
|
|
|
|
"""An error from a general matrix endpoint, eg. from a proxied Matrix API call.
|
2017-03-13 09:50:16 -04:00
|
|
|
|
2018-08-01 09:58:16 -04:00
|
|
|
Attributes:
|
|
|
|
errcode (str): Matrix error code e.g 'M_FORBIDDEN'
|
|
|
|
"""
|
|
|
|
def __init__(self, code, msg, errcode=Codes.UNKNOWN, additional_fields=None):
|
|
|
|
super(ProxiedRequestError, self).__init__(
|
|
|
|
code, msg, errcode
|
|
|
|
)
|
|
|
|
if additional_fields is None:
|
|
|
|
self._additional_fields = {}
|
|
|
|
else:
|
|
|
|
self._additional_fields = dict(additional_fields)
|
2017-03-13 09:50:16 -04:00
|
|
|
|
2018-08-01 09:58:16 -04:00
|
|
|
def error_dict(self):
|
|
|
|
return cs_error(
|
|
|
|
self.msg,
|
|
|
|
self.errcode,
|
|
|
|
**self._additional_fields
|
|
|
|
)
|
2017-03-13 09:50:16 -04:00
|
|
|
|
2014-10-30 07:10:17 -04:00
|
|
|
|
2018-05-22 03:56:52 -04:00
|
|
|
class ConsentNotGivenError(SynapseError):
|
|
|
|
"""The error returned to the client when the user has not consented to the
|
|
|
|
privacy policy.
|
|
|
|
"""
|
|
|
|
def __init__(self, msg, consent_uri):
|
|
|
|
"""Constructs a ConsentNotGivenError
|
|
|
|
|
|
|
|
Args:
|
|
|
|
msg (str): The human-readable error message
|
|
|
|
consent_url (str): The URL where the user can give their consent
|
|
|
|
"""
|
|
|
|
super(ConsentNotGivenError, self).__init__(
|
|
|
|
code=http_client.FORBIDDEN,
|
|
|
|
msg=msg,
|
|
|
|
errcode=Codes.CONSENT_NOT_GIVEN
|
|
|
|
)
|
|
|
|
self._consent_uri = consent_uri
|
|
|
|
|
|
|
|
def error_dict(self):
|
|
|
|
return cs_error(
|
|
|
|
self.msg,
|
|
|
|
self.errcode,
|
|
|
|
consent_uri=self._consent_uri
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
class RegistrationError(SynapseError):
|
|
|
|
"""An error raised when a registration event fails."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-01-22 13:11:18 -05:00
|
|
|
class FederationDeniedError(SynapseError):
|
|
|
|
"""An error raised when the server tries to federate with a server which
|
|
|
|
is not on its federation whitelist.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
destination (str): The destination which has been denied
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, destination):
|
|
|
|
"""Raised by federation client or server to indicate that we are
|
|
|
|
are deliberately not attempting to contact a given server because it is
|
|
|
|
not on our federation whitelist.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
destination (str): the domain in question
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.destination = destination
|
|
|
|
|
|
|
|
super(FederationDeniedError, self).__init__(
|
|
|
|
code=403,
|
|
|
|
msg="Federation denied with %s." % (self.destination,),
|
|
|
|
errcode=Codes.FORBIDDEN,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-12-04 10:47:27 -05:00
|
|
|
class InteractiveAuthIncompleteError(Exception):
|
|
|
|
"""An error raised when UI auth is not yet complete
|
|
|
|
|
|
|
|
(This indicates we should return a 401 with 'result' as the body)
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
result (dict): the server response to the request, which should be
|
|
|
|
passed back to the client
|
|
|
|
"""
|
|
|
|
def __init__(self, result):
|
|
|
|
super(InteractiveAuthIncompleteError, self).__init__(
|
|
|
|
"Interactive auth not yet complete",
|
|
|
|
)
|
|
|
|
self.result = result
|
|
|
|
|
|
|
|
|
2015-01-22 12:38:53 -05: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 14:32:17 -05:00
|
|
|
kwargs["errcode"] = Codes.UNRECOGNIZED
|
2015-01-23 06:19:02 -05:00
|
|
|
message = None
|
|
|
|
if len(args) == 0:
|
|
|
|
message = "Unrecognized request"
|
|
|
|
else:
|
|
|
|
message = args[0]
|
2015-01-22 12:38:53 -05:00
|
|
|
super(UnrecognizedRequestError, self).__init__(
|
|
|
|
400,
|
2015-01-23 06:19:02 -05:00
|
|
|
message,
|
2015-01-22 12:38:53 -05:00
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
2015-01-22 14:32:17 -05:00
|
|
|
|
|
|
|
class NotFoundError(SynapseError):
|
|
|
|
"""An error indicating we can't find the thing you asked for"""
|
2017-03-14 09:36:06 -04:00
|
|
|
def __init__(self, msg="Not found", errcode=Codes.NOT_FOUND):
|
2015-01-23 05:32:40 -05:00
|
|
|
super(NotFoundError, self).__init__(
|
2015-01-22 14:32:17 -05:00
|
|
|
404,
|
2017-03-14 09:36:06 -04:00
|
|
|
msg,
|
|
|
|
errcode=errcode
|
2015-01-22 14:32:17 -05:00
|
|
|
)
|
|
|
|
|
2015-01-29 11:10:35 -05:00
|
|
|
|
2014-08-12 10:10:52 -04: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)
|
|
|
|
|
|
|
|
|
2018-08-16 13:02:02 -04:00
|
|
|
class ResourceLimitError(SynapseError):
|
|
|
|
"""
|
|
|
|
Any error raised when there is a problem with resource usage.
|
|
|
|
For instance, the monthly active user limit for the server has been exceeded
|
|
|
|
"""
|
|
|
|
def __init__(
|
|
|
|
self, code, msg,
|
2018-08-18 09:39:45 -04:00
|
|
|
errcode=Codes.RESOURCE_LIMIT_EXCEEDED,
|
2018-08-24 11:51:27 -04:00
|
|
|
admin_contact=None,
|
2018-08-16 13:02:02 -04:00
|
|
|
limit_type=None,
|
|
|
|
):
|
2018-08-24 11:51:27 -04:00
|
|
|
self.admin_contact = admin_contact
|
2018-08-16 13:02:02 -04:00
|
|
|
self.limit_type = limit_type
|
|
|
|
super(ResourceLimitError, self).__init__(code, msg, errcode=errcode)
|
2018-08-13 13:00:23 -04:00
|
|
|
|
|
|
|
def error_dict(self):
|
|
|
|
return cs_error(
|
|
|
|
self.msg,
|
|
|
|
self.errcode,
|
2018-08-24 11:51:27 -04:00
|
|
|
admin_contact=self.admin_contact,
|
2018-08-16 13:02:02 -04:00
|
|
|
limit_type=self.limit_type
|
2018-08-13 13:00:23 -04:00
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
|
2015-10-22 06:44:31 -04: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 10:10:52 -04:00
|
|
|
class EventStreamError(SynapseError):
|
|
|
|
"""An error raised when there a problem with the event stream."""
|
2014-08-14 06:57:25 -04: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 10:10:52 -04: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-05 22:18:23 -04: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 07:10:17 -04:00
|
|
|
|
2014-09-03 03:58:48 -04: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
|
|
|
|
|
|
|
|
def error_dict(self):
|
2014-08-12 10:10:52 -04:00
|
|
|
return cs_error(
|
2014-09-03 03:58:48 -04:00
|
|
|
self.msg,
|
|
|
|
self.errcode,
|
|
|
|
retry_after_ms=self.retry_after_ms,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-12-05 20:02:57 -05:00
|
|
|
class RoomKeysVersionError(SynapseError):
|
|
|
|
"""A client has tried to upload to a non-current version of the room_keys store
|
|
|
|
"""
|
2017-12-17 20:52:46 -05:00
|
|
|
def __init__(self, current_version):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
current_version (str): the current version of the store they should have used
|
|
|
|
"""
|
|
|
|
super(RoomKeysVersionError, self).__init__(
|
|
|
|
403, "Wrong room_keys version", Codes.WRONG_ROOM_KEYS_VERSION
|
|
|
|
)
|
2017-12-05 20:02:57 -05:00
|
|
|
self.current_version = current_version
|
|
|
|
|
2018-09-06 11:23:16 -04:00
|
|
|
|
2018-07-25 17:25:41 -04:00
|
|
|
class IncompatibleRoomVersionError(SynapseError):
|
|
|
|
"""A server is trying to join a room whose version it does not support."""
|
|
|
|
|
|
|
|
def __init__(self, room_version):
|
|
|
|
super(IncompatibleRoomVersionError, self).__init__(
|
|
|
|
code=400,
|
|
|
|
msg="Your homeserver does not support the features required to "
|
|
|
|
"join this room",
|
|
|
|
errcode=Codes.INCOMPATIBLE_ROOM_VERSION,
|
|
|
|
)
|
|
|
|
|
|
|
|
self._room_version = room_version
|
|
|
|
|
2017-12-05 20:02:57 -05:00
|
|
|
def error_dict(self):
|
|
|
|
return cs_error(
|
|
|
|
self.msg,
|
|
|
|
self.errcode,
|
2018-10-09 13:04:21 -04:00
|
|
|
room_version=self._room_version,
|
2017-12-05 20:02:57 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-08-12 10:10:52 -04: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.
|
2018-05-22 03:56:52 -04:00
|
|
|
code (str): The error code.
|
2014-08-12 10:10:52 -04:00
|
|
|
kwargs : Additional keys to add to the response.
|
|
|
|
Returns:
|
|
|
|
A dict representing the error response JSON.
|
|
|
|
"""
|
|
|
|
err = {"error": msg, "errcode": code}
|
2018-04-15 10:51:07 -04:00
|
|
|
for key, value in iteritems(kwargs):
|
2014-08-12 10:10:52 -04:00
|
|
|
err[key] = value
|
|
|
|
return err
|
2014-11-04 10:10:27 -05: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 11:28:12 -05:00
|
|
|
|
|
|
|
|
|
|
|
class HttpResponseException(CodeMessageException):
|
2017-03-13 09:50:16 -04:00
|
|
|
"""
|
|
|
|
Represents an HTTP-level failure of an outbound request
|
|
|
|
|
|
|
|
Attributes:
|
2018-08-01 09:58:16 -04:00
|
|
|
response (bytes): body of response
|
2017-03-13 09:50:16 -04:00
|
|
|
"""
|
2015-02-04 11:28:12 -05:00
|
|
|
def __init__(self, code, msg, response):
|
2017-03-13 09:50:16 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
Args:
|
|
|
|
code (int): HTTP status code
|
|
|
|
msg (str): reason phrase from HTTP response status line
|
2018-08-01 09:58:16 -04:00
|
|
|
response (bytes): body of response
|
2017-03-13 09:50:16 -04:00
|
|
|
"""
|
2017-03-14 08:36:50 -04:00
|
|
|
super(HttpResponseException, self).__init__(code, msg)
|
2015-02-04 11:28:12 -05:00
|
|
|
self.response = response
|
2018-08-01 09:58:16 -04:00
|
|
|
|
|
|
|
def to_synapse_error(self):
|
|
|
|
"""Make a SynapseError based on an HTTPResponseException
|
|
|
|
|
|
|
|
This is useful when a proxied request has failed, and we need to
|
|
|
|
decide how to map the failure onto a matrix error to send back to the
|
|
|
|
client.
|
|
|
|
|
|
|
|
An attempt is made to parse the body of the http response as a matrix
|
|
|
|
error. If that succeeds, the errcode and error message from the body
|
|
|
|
are used as the errcode and error message in the new synapse error.
|
|
|
|
|
|
|
|
Otherwise, the errcode is set to M_UNKNOWN, and the error message is
|
|
|
|
set to the reason code from the HTTP response.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
SynapseError:
|
|
|
|
"""
|
|
|
|
# try to parse the body as json, to get better errcode/msg, but
|
|
|
|
# default to M_UNKNOWN with the HTTP status as the error text
|
|
|
|
try:
|
|
|
|
j = json.loads(self.response)
|
|
|
|
except ValueError:
|
|
|
|
j = {}
|
|
|
|
|
|
|
|
if not isinstance(j, dict):
|
|
|
|
j = {}
|
|
|
|
|
|
|
|
errcode = j.pop('errcode', Codes.UNKNOWN)
|
|
|
|
errmsg = j.pop('error', self.msg)
|
|
|
|
|
|
|
|
return ProxiedRequestError(self.code, errmsg, errcode, j)
|