diff --git a/changelog.d/12703.misc b/changelog.d/12703.misc new file mode 100644 index 000000000000..ce926fb4b4ed --- /dev/null +++ b/changelog.d/12703.misc @@ -0,0 +1 @@ +Introduce a string enum `Code` to replace the namespace class `Codes`. \ No newline at end of file diff --git a/synapse/api/errors.py b/synapse/api/errors.py index cb3b7323d568..d570dea576b6 100644 --- a/synapse/api/errors.py +++ b/synapse/api/errors.py @@ -17,6 +17,7 @@ import logging import typing +from enum import Enum from http import HTTPStatus from typing import Any, Dict, List, Optional, Union @@ -30,7 +31,17 @@ logger = logging.getLogger(__name__) -class Codes: +class Code(str, Enum): + """ + All known error codes, as an enum of strings. + """ + + def __str__(self) -> str: + return self.value + + def __repr__(self) -> str: + return repr(self.value) + UNRECOGNIZED = "M_UNRECOGNIZED" UNAUTHORIZED = "M_UNAUTHORIZED" FORBIDDEN = "M_FORBIDDEN" @@ -82,6 +93,11 @@ class Codes: UNREDACTED_CONTENT_DELETED = "FI.MAU.MSC2815_UNREDACTED_CONTENT_DELETED" +# `Codes` used to be a namespace for codes. This is now replaced +# with the enum `Code` but we maintain it for backwards compatibility. +Codes = Code + + class CodeMessageException(RuntimeError): """An exception with integer code and message string attributes. @@ -265,7 +281,9 @@ class UnrecognizedRequestError(SynapseError): """An error indicating we don't understand the request you're trying to make""" def __init__( - self, msg: str = "Unrecognized request", errcode: str = Codes.UNRECOGNIZED + self, + msg: str = "Unrecognized request", + errcode: str = Codes.UNRECOGNIZED, ): super().__init__(400, msg, errcode)