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

Commit

Permalink
Introduce an enum Code to replace the namespace class Codes.
Browse files Browse the repository at this point in the history
This is a first step towards a refactoring of the Spam-checker API
towards more uniform and more powerful API/type signatures.
  • Loading branch information
Yoric committed May 20, 2022
1 parent 37935b5 commit 73bc9dd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.d/12703.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Introduce a string enum `Code` to replace the namespace class `Codes`.
22 changes: 20 additions & 2 deletions synapse/api/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import logging
import typing
from enum import Enum
from http import HTTPStatus
from typing import Any, Dict, List, Optional, Union

Expand All @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 73bc9dd

Please sign in to comment.