@@ -91,7 +91,7 @@ def _request(self, url: str, body: Dict[str, Any], response_type: type) -> Any:
9191 Parsed response as Pydantic model
9292
9393 Raises:
94- Exception : If request fails
94+ TurnkeyNetworkError : If request fails
9595 """
9696 full_url = self .base_url + url
9797 body_str = self ._serialize_body (body )
@@ -103,16 +103,30 @@ def _request(self, url: str, body: Dict[str, Any], response_type: type) -> Any:
103103 "X-Client-Version" : VERSION ,
104104 }
105105
106- response = requests .post (
107- full_url , headers = headers , data = body_str , timeout = self .default_timeout
108- )
106+ try :
107+ response = requests .post (
108+ full_url , headers = headers , data = body_str , timeout = self .default_timeout
109+ )
110+ except requests .RequestException as exc :
111+ raise TurnkeyNetworkError (
112+ "Request failed" , None , TurnkeyErrorCodes .NETWORK_ERROR , str (exc )
113+ ) from exc
109114
110115 if not response .ok :
111116 try :
112117 error_data = response .json ()
113- raise Exception ( f"Turnkey API error: { error_data } " )
118+ error_message = error_data . get ( "message" , str ( error_data ) )
114119 except ValueError :
115- raise Exception (f"{ response .status_code } { response .reason } " )
120+ error_message = (
121+ response .text or f"{ response .status_code } { response .reason } "
122+ )
123+
124+ raise TurnkeyNetworkError (
125+ error_message ,
126+ response .status_code ,
127+ TurnkeyErrorCodes .BAD_RESPONSE ,
128+ response .text ,
129+ )
116130
117131 response_data = response .json ()
118132 return response_type (** response_data )
0 commit comments