Skip to content

Commit 65e4165

Browse files
committed
returned custom TurnkeyNetworkError
1 parent cb8e9d8 commit 65e4165

File tree

5 files changed

+60
-21
lines changed

5 files changed

+60
-21
lines changed

codegen/http/generate_http.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def _request(self, url: str, body: Dict[str, Any], response_type: type) -> Any:
104104
Parsed response as Pydantic model
105105
106106
Raises:
107-
Exception: If request fails
107+
TurnkeyNetworkError: If request fails
108108
\"\"\"
109109
full_url = self.base_url + url
110110
body_str = self._serialize_body(body)
@@ -116,19 +116,34 @@ def _request(self, url: str, body: Dict[str, Any], response_type: type) -> Any:
116116
"X-Client-Version": VERSION
117117
}
118118
119-
response = requests.post(
120-
full_url,
121-
headers=headers,
122-
data=body_str,
123-
timeout=self.default_timeout
124-
)
119+
try:
120+
response = requests.post(
121+
full_url,
122+
headers=headers,
123+
data=body_str,
124+
timeout=self.default_timeout
125+
)
126+
except requests.RequestException as exc:
127+
raise TurnkeyNetworkError(
128+
"Request failed",
129+
None,
130+
TurnkeyErrorCodes.NETWORK_ERROR,
131+
str(exc)
132+
) from exc
125133
126134
if not response.ok:
127135
try:
128136
error_data = response.json()
129-
raise Exception(f"Turnkey API error: {error_data}")
137+
error_message = error_data.get("message", str(error_data))
130138
except ValueError:
131-
raise Exception(f"{response.status_code} {response.reason}")
139+
error_message = response.text or f"{response.status_code} {response.reason}"
140+
141+
raise TurnkeyNetworkError(
142+
error_message,
143+
response.status_code,
144+
TurnkeyErrorCodes.BAD_RESPONSE,
145+
response.text
146+
)
132147
133148
response_data = response.json()
134149
return response_type(**response_data)

packages/http/src/turnkey_http/generated/client.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

packages/http/tests/test_activities.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
v1ApiKeyCurve,
88
TCreateApiKeysBody,
99
TGetActivityResponse,
10+
TurnkeyNetworkError,
1011
)
1112
from turnkey_http.utils import send_signed_request
1213

@@ -80,16 +81,18 @@ def test_organization_id_override(client, user_id):
8081
)
8182

8283
# This should fail because we're using a wrong organization ID
83-
with pytest.raises(Exception) as exc_info:
84+
with pytest.raises(TurnkeyNetworkError) as exc_info:
8485
client.create_api_keys(request)
8586

8687
# Verify the error is related to the wrong organization
87-
error_msg = str(exc_info.value)
88+
error = exc_info.value
89+
error_msg = str(error)
8890
print(f"\n❌ Error message: {error_msg}")
91+
print(f" Status code: {error.status_code}")
8992
print(f"✅ Request failed as expected with wrong organization ID")
9093

9194
# Assert that we got the expected error for organization not found
92-
assert "ORGANIZATION_NOT_FOUND" in error_msg
95+
assert "no organization found" in error_msg.lower()
9396
assert wrong_org_id in error_msg
9497

9598

packages/http/tests/test_queries.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
"""Test Turnkey HTTP client query methods"""
22

33
from turnkey_http.utils import send_signed_request
4-
from turnkey_sdk_types import TGetOrganizationResponse, TGetOrganizationBody
4+
from turnkey_sdk_types import (
5+
TGetOrganizationResponse,
6+
TGetOrganizationBody,
7+
TurnkeyNetworkError,
8+
)
59

610
import pytest
711

@@ -106,12 +110,14 @@ def test_organization_id_override_query(client):
106110
request = TGetOrganizationBody(organizationId=wrong_org_id)
107111

108112
# This should fail because we're using a wrong organization ID
109-
with pytest.raises(Exception) as exc_info:
113+
with pytest.raises(TurnkeyNetworkError) as exc_info:
110114
client.get_organization(request)
111115

112116
# Verify the error is related to the wrong organization
113-
error_msg = str(exc_info.value)
117+
error = exc_info.value
118+
error_msg = str(error)
114119
print(f"\n❌ Error message: {error_msg}")
120+
print(f" Status code: {error.status_code}")
115121
print(f"✅ Request failed as expected with wrong organization ID")
116122

117123
# Assert that we got an error for invalid organization (different error than activities)

packages/sdk-types/tests/test_pydantic_aliases.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
TEthSendTransactionBody,
55
)
66

7+
78
def test_pydantic_aliases():
89
"""Test Pydantic field aliasing for Python keywords and special characters.
910

0 commit comments

Comments
 (0)