Base exception for all **handled/known** API errors. All user-facing error responses should inherit from this class (or use its subclasses) so they get consistent formatting.
| 7 | |
| 8 | |
| 9 | class APIException(HTTPException): |
| 10 | """ |
| 11 | Base exception for all **handled/known** API errors. |
| 12 | |
| 13 | All user-facing error responses should inherit from this class |
| 14 | (or use its subclasses) so they get consistent formatting. |
| 15 | """ |
| 16 | |
| 17 | def __init__( |
| 18 | self, |
| 19 | status_code: int, |
| 20 | detail: str, |
| 21 | error_code: str | None = None, |
| 22 | data: Any = None, |
| 23 | headers: dict[str, str] | None = None, |
| 24 | ): |
| 25 | self.error_code = error_code or self.__class__.__name__.replace("Exception", "").upper() |
| 26 | self.data = data |
| 27 | |
| 28 | # We put everything inside 'detail' so the frontend gets a rich object |
| 29 | rich_detail = { |
| 30 | "message": detail, |
| 31 | "error_code": self.error_code, |
| 32 | } |
| 33 | if data is not None: |
| 34 | rich_detail["data"] = data |
| 35 | |
| 36 | super().__init__( |
| 37 | status_code=status_code, |
| 38 | detail=rich_detail, |
| 39 | headers=headers, |
| 40 | ) |
| 41 | |
| 42 | |
| 43 | # ──────────────────────────────────────── |
no outgoing calls
no test coverage detected