Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Specific an error code for RoomAlias.from_string.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Feb 27, 2020
1 parent 82d3585 commit 32bdbb3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions synapse/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from signedjson.key import decode_verify_key_bytes
from unpaddedbase64 import decode_base64

from synapse.api.errors import SynapseError
from synapse.api.errors import Codes, SynapseError

# define a version of typing.Collection that works on python 3.5
if sys.version_info[:3] >= (3, 6, 0):
Expand Down Expand Up @@ -166,11 +166,13 @@ def __deepcopy__(self, memo):
return self

@classmethod
def from_string(cls, s):
def from_string(cls, s: str):
"""Parse the string given by 's' into a structure object."""
if len(s) < 1 or s[0:1] != cls.SIGIL:
raise SynapseError(
400, "Expected %s string to start with '%s'" % (cls.__name__, cls.SIGIL)
400,
"Expected %s string to start with '%s'" % (cls.__name__, cls.SIGIL),
Codes.INVALID_PARAM,
)

parts = s[1:].split(":", 1)
Expand All @@ -179,6 +181,7 @@ def from_string(cls, s):
400,
"Expected %s of the form '%slocalname:domain'"
% (cls.__name__, cls.SIGIL),
Codes.INVALID_PARAM,
)

domain = parts[1]
Expand Down Expand Up @@ -235,11 +238,13 @@ class GroupID(DomainSpecificString):
def from_string(cls, s):
group_id = super(GroupID, cls).from_string(s)
if not group_id.localpart:
raise SynapseError(400, "Group ID cannot be empty")
raise SynapseError(400, "Group ID cannot be empty", Codes.INVALID_PARAM)

if contains_invalid_mxid_characters(group_id.localpart):
raise SynapseError(
400, "Group ID can only contain characters a-z, 0-9, or '=_-./'"
400,
"Group ID can only contain characters a-z, 0-9, or '=_-./'",
Codes.INVALID_PARAM,
)

return group_id
Expand Down
2 changes: 1 addition & 1 deletion tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_validate(self):
self.fail("Parsing '%s' should raise exception" % id_string)
except SynapseError as exc:
self.assertEqual(400, exc.code)
self.assertEqual("M_UNKNOWN", exc.errcode)
self.assertEqual("M_INVALID_PARAM", exc.errcode)


class MapUsernameTestCase(unittest.TestCase):
Expand Down

0 comments on commit 32bdbb3

Please sign in to comment.