[docs]classBaseApiException(Exception):""" Exception class for API related errors. """message:str
[docs]classApiResponseException(BaseApiException):""" Exception class for API with valid response body. """response:BaseErrorResponse
[docs]def__init__(self,response:Union[BaseErrorResponse,dict],message:Optional[str]=None,*args,)->None:ifis_api_error_response(response):self.response=responseelifisinstance(response,dict):self.response=to_api_error(response)else:raiseTypeError(f"Expected either Response or Api Error Response, but {type(response)} received.")self.message=f"{messageor'Server Error'}\n{self.response.model_dump_json(indent=2)}"super().__init__(self.message,*args)
[docs]@classmethoddeffrom_http_response(cls,response:Response,message:Optional[str]=None):ifresponse.is_success:raiseValueError("Cannot convert succeed HTTP response to error response.")response_body=to_api_error(response.json())returncls(message=message,response=response_body)
[docs]classApiNetworkException(BaseApiException):""" Exception raised when there is a network-related error during API communication ('httpx' related error). Attributes: message (str): Explanation of the error. """__cause__:Optional[HTTPError]=None
[docs]def__init__(self,message:Optional[str]=None,*args)->None:self.message=messageor"Network Exception has occurred. Try again later."super().__init__(self.message,*args)