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

Commit 73bc9dd

Browse files
committed
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.
1 parent 37935b5 commit 73bc9dd

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

changelog.d/12703.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Introduce a string enum `Code` to replace the namespace class `Codes`.

synapse/api/errors.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import logging
1919
import typing
20+
from enum import Enum
2021
from http import HTTPStatus
2122
from typing import Any, Dict, List, Optional, Union
2223

@@ -30,7 +31,17 @@
3031
logger = logging.getLogger(__name__)
3132

3233

33-
class Codes:
34+
class Code(str, Enum):
35+
"""
36+
All known error codes, as an enum of strings.
37+
"""
38+
39+
def __str__(self) -> str:
40+
return self.value
41+
42+
def __repr__(self) -> str:
43+
return repr(self.value)
44+
3445
UNRECOGNIZED = "M_UNRECOGNIZED"
3546
UNAUTHORIZED = "M_UNAUTHORIZED"
3647
FORBIDDEN = "M_FORBIDDEN"
@@ -82,6 +93,11 @@ class Codes:
8293
UNREDACTED_CONTENT_DELETED = "FI.MAU.MSC2815_UNREDACTED_CONTENT_DELETED"
8394

8495

96+
# `Codes` used to be a namespace for codes. This is now replaced
97+
# with the enum `Code` but we maintain it for backwards compatibility.
98+
Codes = Code
99+
100+
85101
class CodeMessageException(RuntimeError):
86102
"""An exception with integer code and message string attributes.
87103
@@ -265,7 +281,9 @@ class UnrecognizedRequestError(SynapseError):
265281
"""An error indicating we don't understand the request you're trying to make"""
266282

267283
def __init__(
268-
self, msg: str = "Unrecognized request", errcode: str = Codes.UNRECOGNIZED
284+
self,
285+
msg: str = "Unrecognized request",
286+
errcode: str = Codes.UNRECOGNIZED,
269287
):
270288
super().__init__(400, msg, errcode)
271289

0 commit comments

Comments
 (0)