From adb64d863b5c0d1fa85b8bbf18a9c55846b0f261 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Fri, 6 Dec 2024 14:12:36 -0500 Subject: [PATCH] Match the MSC error codes. --- synapse/api/errors.py | 4 ++++ synapse/rest/client/profile.py | 6 ++++-- synapse/storage/databases/main/profile.py | 4 ++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/synapse/api/errors.py b/synapse/api/errors.py index 21989b6e0e..5dd6e84289 100644 --- a/synapse/api/errors.py +++ b/synapse/api/errors.py @@ -132,6 +132,10 @@ class Codes(str, Enum): # connection. UNKNOWN_POS = "M_UNKNOWN_POS" + # Part of MSC4133 + PROFILE_TOO_LARGE = "M_PROFILE_TOO_LARGE" + KEY_TOO_LARGE = "M_KEY_TOO_LARGE" + class CodeMessageException(RuntimeError): """An exception with integer code, a message string attributes and optional headers. diff --git a/synapse/rest/client/profile.py b/synapse/rest/client/profile.py index 7363259653..fed4e750b5 100644 --- a/synapse/rest/client/profile.py +++ b/synapse/rest/client/profile.py @@ -292,7 +292,9 @@ class UnstableProfileRestServlet(RestServlet): if len(field_name.encode("utf-8")) > MAX_CUSTOM_FIELD_LEN: raise SynapseError( - 400, f"Field name too long: {field_name}", errcode=Codes.TOO_LARGE + 400, + f"Field name too long: {field_name}", + errcode=Codes.KEY_TOO_LARGE, ) propagate = _read_propagate(self.hs, request) @@ -441,7 +443,7 @@ class UnstableProfileFieldRestServlet(RestServlet): raise SynapseError(400, "Field name too short", errcode=Codes.INVALID_PARAM) if len(field_name.encode("utf-8")) > MAX_CUSTOM_FIELD_LEN: - raise SynapseError(400, "Field name too long", errcode=Codes.TOO_LARGE) + raise SynapseError(400, "Field name too long", errcode=Codes.KEY_TOO_LARGE) propagate = _read_propagate(self.hs, request) diff --git a/synapse/storage/databases/main/profile.py b/synapse/storage/databases/main/profile.py index 5a753ab1ae..23e9baea48 100644 --- a/synapse/storage/databases/main/profile.py +++ b/synapse/storage/databases/main/profile.py @@ -24,7 +24,7 @@ from typing import TYPE_CHECKING, Dict, Optional, Tuple, cast from canonicaljson import encode_canonical_json from synapse.api.constants import ProfileFields -from synapse.api.errors import StoreError +from synapse.api.errors import Codes, StoreError from synapse.storage._base import SQLBaseStore from synapse.storage.database import ( DatabasePool, @@ -366,7 +366,7 @@ class ProfileWorkerStore(SQLBaseStore): total_bytes += len(encode_canonical_json({new_field_name: new_value})) if total_bytes > MAX_PROFILE_SIZE: - raise StoreError(400, "Profile too large") + raise StoreError(400, "Profile too large", Codes.PROFILE_TOO_LARGE) async def set_profile_displayname( self, user_id: UserID, new_displayname: Optional[str]