用另一个枚举作为钥匙枚举

发布于 2025-02-11 23:08:23 字数 1477 浏览 1 评论 0原文

我正在从事一个项目从未提出例外的项目。取而代之的是,它返回一个结果对象:

Dto = TypeVar("Dto", bound=BaseModel)


class BaseError(BaseModel):
    error_code: DomainErrorEnum
    message: str

    def raise_error(self):
        raise ErrorMap[self.error_code] # Here is the big deal


class Result(BaseModel, Generic[Dto]):
    error = Optional[BaseError]
    data: Optional[Dto]

其中domainerrorenum是映射代表我们域名的slugs的枚举。

我喜欢做的是errormap类似这样的类:

class ErrorMap(DomainErrorEnum, Enum):
    DomainErrorEnum.GENERIC_ERROR = GenericErrorException
    DomainErrorEnum.NOT_FOUND = CartNotFoundException
    DomainErrorEnum.OUT_OF_STOCK = OutOfStockException

这样做,我有2件事很好地隔离:

  1. 枚举(实际上我们有许多“ domainerrorenum”,但是我在这里假装我们只有一个可以使我的榜样易于理解)具有代表(slugs)我所有域错误的责任的
  2. 责任提出任何东西,它总是返回结果[特定DTO]

如何表示这2件事的想法?

# On domain module:
def specific_use_case():
    result_search: Result[ProductDto] = adapter.search_product()
    if product_dto.is_success:
        data: ProductDto = result_search.data
        result_search.business_rules_method(data) # Here, on pure business rules we throw exceptions without using our map, because here we dont use these Result pattern
        return entity

    result_search.error.raise_error()
    

Im working on a project where adapters never raise Exceptions. Instead, it returns a Result object like this:

Dto = TypeVar("Dto", bound=BaseModel)


class BaseError(BaseModel):
    error_code: DomainErrorEnum
    message: str

    def raise_error(self):
        raise ErrorMap[self.error_code] # Here is the big deal


class Result(BaseModel, Generic[Dto]):
    error = Optional[BaseError]
    data: Optional[Dto]

Where DomainErrorEnum is an Enum that maps slugs that represent our DomainErrors.

What I like to do is a ErrorMap class that works like this:

class ErrorMap(DomainErrorEnum, Enum):
    DomainErrorEnum.GENERIC_ERROR = GenericErrorException
    DomainErrorEnum.NOT_FOUND = CartNotFoundException
    DomainErrorEnum.OUT_OF_STOCK = OutOfStockException

Doing this, I have 2 things well segregated:

  1. An Enum (actually we have many "DomainErrorEnum", but Im pretending here that we have only one to make my example easy to understand) that has the responsability to represents (slugs) all my domain errors
  2. A class that know which Exception it should throw whatever I want to trhow it (remember, my adapters never raise anything, it always return a Result[SpecificDto] object

Any idea on how to represent these 2 things?

Just for clarify:

# On domain module:
def specific_use_case():
    result_search: Result[ProductDto] = adapter.search_product()
    if product_dto.is_success:
        data: ProductDto = result_search.data
        result_search.business_rules_method(data) # Here, on pure business rules we throw exceptions without using our map, because here we dont use these Result pattern
        return entity

    result_search.error.raise_error()
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

标点 2025-02-18 23:08:23

errormap可能应该 be 映射(字典):

ERROR_MAP = {
    DomainErrorEnum.GENERIC_ERROR: GenericErrorException,
    DomainErrorEnum.NOT_FOUND: CartNotFoundException,
    DomainErrorEnum.OUT_OF_STOCK: OutOfStockException
}

ErrorMap should probably be a map (a dictionary):

ERROR_MAP = {
    DomainErrorEnum.GENERIC_ERROR: GenericErrorException,
    DomainErrorEnum.NOT_FOUND: CartNotFoundException,
    DomainErrorEnum.OUT_OF_STOCK: OutOfStockException
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文