from typing import Any from fastapi import HTTPException class TranslatedHTTPException(HTTPException): """HTTP error carrying a translation key instead of fixed text. The application exception handler translates the key into the language of the current user. ``detail`` holds the English default so handlers without translation support (for example the callback-only service) still return readable text. """ def __init__(self, status_code: int, key: str, default: str, **values: Any) -> None: try: detail = default.format(**values) except (KeyError, ValueError): detail = default super().__init__(status_code, detail) self.translation_key = key self.translation_default = default self.translation_values = values