Coverage for app/core/base/base_error.py: 100%
18 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-15 01:44 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-15 01:44 +0000
1# from collections.abc import Mapping
2from typing import Any
4from app.ddd.infrastructure.util import recursive_to_camel
7class BaseError(Exception):
8 def __init__(self, error_code: str, status_code: int, description: str, parameters: dict[str, Any] | None = None) -> None:
9 """
10 BaseError.
12 継承して利用するアプリエラークラス
14 Args:
15 error_code: ERROR CODE
16 status_code: HTTP STATUS CODE
17 description: エラー内容の説明(parametersでフォーマットさせる)
18 parameters: その他パラメータ(エラーメッセージに追加する文言など)
20 """
21 super().__init__(description)
22 self.__error_code: str = error_code # application error code
23 self.__status_code: int = status_code # http status code
24 self.__parameter: dict[str, Any] = parameters or {}
25 self.__message: dict[str, Any] = {
26 "description": description.format(**self.__parameter),
27 } | self.__parameter
28 self.__detail: dict[str, Any] = {
29 "error_code": self.__error_code,
30 "status_code": self.__status_code,
31 } | self.__message
33 # def __str__(self) -> str:
34 # return json.dumps(self.__detail)
36 # def error_code(self) -> str:
37 # return self.__error_code
39 def status_code(self) -> int:
40 return self.__status_code
42 def detail(self) -> dict[str, Any]:
43 return self.__detail
45 def camel_detail(self) -> dict[str, Any]:
46 return recursive_to_camel(self.detail())
48 def response(self) -> dict[str, Any]:
49 """エラーレスポンスの内容を生成."""
50 return {
51 "content": {
52 "application/json": {
53 "example": {
54 "detail": [
55 self.camel_detail(),
56 ]
57 }
58 }
59 },
60 }