"""Helper Methods for the proxy classes.""" from typing import ( Any, Coroutine, Type, TypeVar, ) from fastapi.exceptions import HTTPException from httpx import HTTPStatusError T = TypeVar("T") async def lookup_with_error_handling( lookup_cb: Coroutine[Any, Any, T] | T, http_error_message_400_string: str, http_error_message_500_string: str, unhandled_exception_string: str, lookup_error_cls: Type[Exception], ) -> T: """Wrapper for proxy lookup methods with error handling.""" try: # This check is here because lookup_parent_companies_by_uuids is sync not async if not isinstance(lookup_cb, Coroutine): return lookup_cb return await lookup_cb except HTTPStatusError as exc: if 400 <= exc.response.status_code < 500: # Convert to type handled by `error_handlers.http_error_handler`. raise HTTPException( status_code=exc.response.status_code, detail=f"msg:'{http_error_message_400_string}' error:'{exc}'", ) raise lookup_error_cls( f"msg:'{http_error_message_500_string}' error:'{exc}'", ) from exc except Exception as exc: raise lookup_error_cls(unhandled_exception_string) from exc