Spaces:
Sleeping
Sleeping
Fix urlopen timeout arg + unwrap ExceptionGroup in error display
Browse files
app.py
CHANGED
|
@@ -279,8 +279,11 @@ async def _gemini_stream(api_key: str, contents: list[dict]):
|
|
| 279 |
method="POST",
|
| 280 |
)
|
| 281 |
try:
|
|
|
|
|
|
|
|
|
|
| 282 |
response = await asyncio.to_thread(
|
| 283 |
-
urllib.request.urlopen
|
| 284 |
except urllib.error.HTTPError as exc:
|
| 285 |
detail = await asyncio.to_thread(exc.read)
|
| 286 |
text = detail.decode("utf-8", "replace")[:600]
|
|
@@ -561,10 +564,21 @@ def answer(question: str):
|
|
| 561 |
yield (f"**{payload[0]}**", ANSWER_PLACEHOLDER, "")
|
| 562 |
elif kind == "error":
|
| 563 |
exc = payload[0]
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 568 |
|
| 569 |
|
| 570 |
# --- UI -----------------------------------------------------------------------
|
|
|
|
| 279 |
method="POST",
|
| 280 |
)
|
| 281 |
try:
|
| 282 |
+
# `timeout` is a kwarg of urlopen; passing it positionally to
|
| 283 |
+
# asyncio.to_thread would forward it as `data` (POST body) and break
|
| 284 |
+
# the request.
|
| 285 |
response = await asyncio.to_thread(
|
| 286 |
+
lambda: urllib.request.urlopen(request, timeout=REQUEST_TIMEOUT))
|
| 287 |
except urllib.error.HTTPError as exc:
|
| 288 |
detail = await asyncio.to_thread(exc.read)
|
| 289 |
text = detail.decode("utf-8", "replace")[:600]
|
|
|
|
| 564 |
yield (f"**{payload[0]}**", ANSWER_PLACEHOLDER, "")
|
| 565 |
elif kind == "error":
|
| 566 |
exc = payload[0]
|
| 567 |
+
# Unwrap ExceptionGroup (from anyio TaskGroups in the MCP client)
|
| 568 |
+
# so the user sees the actual root cause, not the wrapper.
|
| 569 |
+
lines = []
|
| 570 |
+
def _walk(e, depth=0):
|
| 571 |
+
indent = " " * depth
|
| 572 |
+
lines.append(f"{indent}- `{type(e).__name__}: {e}`")
|
| 573 |
+
inner = getattr(e, "exceptions", None)
|
| 574 |
+
if inner:
|
| 575 |
+
for sub in inner:
|
| 576 |
+
_walk(sub, depth + 1)
|
| 577 |
+
_walk(exc)
|
| 578 |
+
yield ("**Could not complete the request.**\n\n"
|
| 579 |
+
+ "\n".join(lines) +
|
| 580 |
+
"\n\nThe MCP service may be waking from sleep -- "
|
| 581 |
+
"try again in a moment.", ANSWER_PLACEHOLDER, "")
|
| 582 |
|
| 583 |
|
| 584 |
# --- UI -----------------------------------------------------------------------
|