From 4657d2867a428ef42322def26bd697c9587b53e9 Mon Sep 17 00:00:00 2001 From: David Teller Date: Fri, 20 May 2022 08:49:40 +0200 Subject: [PATCH 1/3] Introduce an enum `Code` to replace the namespace class `Codes`. This is a first step towards a refactoring of the Spam-checker API towards more uniform and more powerful API/type signatures. --- changelog.d/12703.misc | 1 + synapse/api/errors.py | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 changelog.d/12703.misc 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) From 5e389305961565c307fb08c05688a90db7fb96f7 Mon Sep 17 00:00:00 2001 From: David Teller Date: Fri, 20 May 2022 11:07:42 +0200 Subject: [PATCH 2/3] WIP: Applied feedback --- changelog.d/12703.misc | 2 +- synapse/api/errors.py | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/changelog.d/12703.misc b/changelog.d/12703.misc index ce926fb4b4ed..9aaa1bbaa3d0 100644 --- a/changelog.d/12703.misc +++ b/changelog.d/12703.misc @@ -1 +1 @@ -Introduce a string enum `Code` to replace the namespace class `Codes`. \ No newline at end of file +Convert namespace class `Codes` into a string enum. \ No newline at end of file diff --git a/synapse/api/errors.py b/synapse/api/errors.py index d570dea576b6..a2387418da37 100644 --- a/synapse/api/errors.py +++ b/synapse/api/errors.py @@ -31,7 +31,7 @@ logger = logging.getLogger(__name__) -class Code(str, Enum): +class Codes(str, Enum): """ All known error codes, as an enum of strings. """ @@ -93,11 +93,6 @@ def __repr__(self) -> str: 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. From 0aeff5e9406d57f2546dcf852173d8fc1ef76c6b Mon Sep 17 00:00:00 2001 From: David Teller Date: Fri, 20 May 2022 12:59:19 +0200 Subject: [PATCH 3/3] WIP: Simplifying Codes --- synapse/api/errors.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/synapse/api/errors.py b/synapse/api/errors.py index a2387418da37..9614be6b4e46 100644 --- a/synapse/api/errors.py +++ b/synapse/api/errors.py @@ -36,12 +36,6 @@ class Codes(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"