re-refactor exception heirarchy

Give CodeMessageException back its `msg` attribute, and use that to hold the
HTTP status message for HttpResponseException.
This commit is contained in:
Richard van der Hoff 2017-03-14 12:36:50 +00:00
parent 170ccc9de5
commit 7f237800e9

View file

@ -55,34 +55,35 @@ class CodeMessageException(RuntimeError):
Attributes: Attributes:
code (int): HTTP error code code (int): HTTP error code
response_code_message (str): HTTP reason phrase. None for the default. msg (str): string describing the error
""" """
def __init__(self, code): def __init__(self, code, msg):
super(CodeMessageException, self).__init__("%d" % code) super(CodeMessageException, self).__init__("%d: %s" % (code, msg))
self.code = code self.code = code
self.response_code_message = None self.msg = msg
def error_dict(self): def error_dict(self):
return cs_error(self.msg) return cs_error(self.msg)
class SynapseError(CodeMessageException): class SynapseError(CodeMessageException):
"""A base error which can be caught for all synapse events.""" """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'
"""
def __init__(self, code, msg, errcode=Codes.UNKNOWN): def __init__(self, code, msg, errcode=Codes.UNKNOWN):
"""Constructs a synapse error. """Constructs a synapse error.
Args: Args:
code (int): The integer error code (an HTTP response code) code (int): The integer error code (an HTTP response code)
msg (str): The human-readable error message. msg (str): The human-readable error message.
errcode (str): The synapse error code e.g 'M_FORBIDDEN' errcode (str): The matrix error code e.g 'M_FORBIDDEN'
""" """
super(SynapseError, self).__init__(code) super(SynapseError, self).__init__(code, msg)
self.msg = msg
self.errcode = errcode self.errcode = errcode
def __str__(self):
return "%d: %s %s" % (self.code, self.errcode, self.msg)
def error_dict(self): def error_dict(self):
return cs_error( return cs_error(
self.msg, self.msg,
@ -106,10 +107,9 @@ class SynapseError(CodeMessageException):
except ValueError: except ValueError:
j = {} j = {}
errcode = j.get('errcode', Codes.UNKNOWN) errcode = j.get('errcode', Codes.UNKNOWN)
errmsg = j.get('error', err.response_code_message) errmsg = j.get('error', err.msg)
res = SynapseError(err.code, errmsg, errcode) res = SynapseError(err.code, errmsg, errcode)
res.response_code_message = err.response_code_message
return res return res
@ -204,7 +204,6 @@ class LimitExceededError(SynapseError):
errcode=Codes.LIMIT_EXCEEDED): errcode=Codes.LIMIT_EXCEEDED):
super(LimitExceededError, self).__init__(code, msg, errcode) super(LimitExceededError, self).__init__(code, msg, errcode)
self.retry_after_ms = retry_after_ms self.retry_after_ms = retry_after_ms
self.response_code_message = "Too Many Requests"
def error_dict(self): def error_dict(self):
return cs_error( return cs_error(
@ -288,6 +287,5 @@ class HttpResponseException(CodeMessageException):
msg (str): reason phrase from HTTP response status line msg (str): reason phrase from HTTP response status line
response (str): body of response response (str): body of response
""" """
super(HttpResponseException, self).__init__(code) super(HttpResponseException, self).__init__(code, msg)
self.response_code_message = msg
self.response = response self.response = response