Skip to content

Commit 65398fd

Browse files
authored
docs: Improve API error handling for toolbox-core client (#275)
* docs: Improve error handling for `toolbox-core` client * fix(toolbox-core): Improve error handling in API response * chore: Add defensive check for response status
1 parent cf1d0ab commit 65398fd

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

packages/toolbox-core/src/toolbox_core/client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515

16-
from asyncio import get_running_loop
1716
from types import MappingProxyType
1817
from typing import Any, Awaitable, Callable, Mapping, Optional, Union
1918

@@ -194,6 +193,11 @@ async def load_tool(
194193
# request the definition of the tool from the server
195194
url = f"{self.__base_url}/api/tool/{name}"
196195
async with self.__session.get(url, headers=resolved_headers) as response:
196+
if not response.ok:
197+
error_text = await response.text()
198+
raise RuntimeError(
199+
f"API request failed with status {response.status} ({response.reason}). Server response: {error_text}"
200+
)
197201
json = await response.json()
198202
manifest: ManifestSchema = ManifestSchema(**json)
199203

@@ -272,6 +276,11 @@ async def load_toolset(
272276
# Request the definition of the toolset from the server
273277
url = f"{self.__base_url}/api/toolset/{name or ''}"
274278
async with self.__session.get(url, headers=resolved_headers) as response:
279+
if not response.ok:
280+
error_text = await response.text()
281+
raise RuntimeError(
282+
f"API request failed with status {response.status} ({response.reason}). Server response: {error_text}"
283+
)
275284
json = await response.json()
276285
manifest: ManifestSchema = ManifestSchema(**json)
277286

packages/toolbox-core/src/toolbox_core/tool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ async def __call__(self, *args: Any, **kwargs: Any) -> str:
289289
headers=headers,
290290
) as resp:
291291
body = await resp.json()
292-
if resp.status < 200 or resp.status >= 300:
292+
if not resp.ok:
293293
err = body.get("error", f"unexpected status from server: {resp.status}")
294294
raise Exception(err)
295295
return body.get("result", body)

0 commit comments

Comments
 (0)