Skip to content

Commit 69a04b7

Browse files
improve error handling
Signed-off-by: Prashant <prashant.rakheja@sap.com>
1 parent 9270862 commit 69a04b7

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/sap_cloud_sdk/agentgateway/agw_client.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ async def list_mcp_tools(
171171
raise
172172
except Exception as e:
173173
logger.exception("Unexpected error during tool discovery")
174-
raise AgentGatewaySDKError(f"Tool discovery failed: {e}") from e
174+
cause = _unwrap_exception_group(e)
175+
raise AgentGatewaySDKError(f"Tool discovery failed: {cause}") from e
175176

176177
@record_metrics(Module.AGENTGATEWAY, Operation.AGENTGATEWAY_CALL_MCP_TOOL)
177178
async def call_mcp_tool(
@@ -264,11 +265,19 @@ async def call_mcp_tool(
264265
raise
265266
except Exception as e:
266267
logger.exception("Unexpected error during tool invocation")
268+
cause = _unwrap_exception_group(e)
267269
raise AgentGatewaySDKError(
268-
f"Tool invocation failed for '{tool.name}': {e}"
270+
f"Tool invocation failed for '{tool.name}': {cause}"
269271
) from e
270272

271273

274+
def _unwrap_exception_group(exc: Exception) -> Exception:
275+
"""Unwrap nested ExceptionGroups to present meaningful error messages."""
276+
while isinstance(exc, BaseExceptionGroup) and exc.exceptions:
277+
exc = exc.exceptions[0]
278+
return exc
279+
280+
272281
def create_client(
273282
tenant_subdomain: str | Callable[[], str] | None = None,
274283
timeout: float = 60.0,

0 commit comments

Comments
 (0)