|
17 | 17 |
|
18 | 18 | import logging |
19 | 19 | import typing |
| 20 | +from enum import Enum |
20 | 21 | from http import HTTPStatus |
21 | 22 | from typing import Any, Dict, List, Optional, Union |
22 | 23 |
|
|
30 | 31 | logger = logging.getLogger(__name__) |
31 | 32 |
|
32 | 33 |
|
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 | + |
34 | 45 | UNRECOGNIZED = "M_UNRECOGNIZED" |
35 | 46 | UNAUTHORIZED = "M_UNAUTHORIZED" |
36 | 47 | FORBIDDEN = "M_FORBIDDEN" |
@@ -82,6 +93,11 @@ class Codes: |
82 | 93 | UNREDACTED_CONTENT_DELETED = "FI.MAU.MSC2815_UNREDACTED_CONTENT_DELETED" |
83 | 94 |
|
84 | 95 |
|
| 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 | + |
85 | 101 | class CodeMessageException(RuntimeError): |
86 | 102 | """An exception with integer code and message string attributes. |
87 | 103 |
|
@@ -265,7 +281,9 @@ class UnrecognizedRequestError(SynapseError): |
265 | 281 | """An error indicating we don't understand the request you're trying to make""" |
266 | 282 |
|
267 | 283 | def __init__( |
268 | | - self, msg: str = "Unrecognized request", errcode: str = Codes.UNRECOGNIZED |
| 284 | + self, |
| 285 | + msg: str = "Unrecognized request", |
| 286 | + errcode: str = Codes.UNRECOGNIZED, |
269 | 287 | ): |
270 | 288 | super().__init__(400, msg, errcode) |
271 | 289 |
|
|
0 commit comments