Update gemini_client/core.py
Browse files- gemini_client/core.py +22 -1
gemini_client/core.py
CHANGED
|
@@ -846,6 +846,26 @@ class AsyncChatbot:
|
|
| 846 |
# Process all parts in this JSON array
|
| 847 |
for part_index, part in enumerate(parsed_json):
|
| 848 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 849 |
if isinstance(part, list) and len(part) > 2:
|
| 850 |
# part[2] might be a string that needs parsing
|
| 851 |
if isinstance(part[2], str):
|
|
@@ -861,7 +881,8 @@ class AsyncChatbot:
|
|
| 861 |
if main_part and isinstance(main_part, list) and len(main_part) > 4 and main_part[4]:
|
| 862 |
body = main_part
|
| 863 |
body_index = part_index
|
| 864 |
-
except (IndexError, TypeError, json.JSONDecodeError, AttributeError):
|
|
|
|
| 865 |
continue
|
| 866 |
|
| 867 |
# If we found a body, stop looking
|
|
|
|
| 846 |
# Process all parts in this JSON array
|
| 847 |
for part_index, part in enumerate(parsed_json):
|
| 848 |
try:
|
| 849 |
+
# Check for error frame ["e", code, ...]
|
| 850 |
+
if isinstance(part, list) and len(part) >= 2 and part[0] == "e" and isinstance(part[1], int):
|
| 851 |
+
error_code = part[1]
|
| 852 |
+
error_status = "Unknown"
|
| 853 |
+
# Map common gRPC error codes
|
| 854 |
+
if error_code == 3: error_status = "INVALID_ARGUMENT"
|
| 855 |
+
elif error_code == 4: error_status = "DEADLINE_EXCEEDED (Request timed out on server)"
|
| 856 |
+
elif error_code == 8: error_status = "RESOURCE_EXHAUSTED"
|
| 857 |
+
elif error_code == 14: error_status = "UNAVAILABLE"
|
| 858 |
+
elif error_code == 16: error_status = "UNAUTHENTICATED"
|
| 859 |
+
|
| 860 |
+
error_msg = f"Server returned error: {error_status} (Code {error_code})"
|
| 861 |
+
if len(part) > 4 and part[4]:
|
| 862 |
+
error_msg += f" - Details: {part[4]}"
|
| 863 |
+
|
| 864 |
+
console.log(f"[bold red]{error_msg}[/bold red]")
|
| 865 |
+
# Don't return immediately, look for other parts? No, "e" typically ends the stream or indicates failure.
|
| 866 |
+
# Return error result
|
| 867 |
+
return {"content": error_msg, "error": True}
|
| 868 |
+
|
| 869 |
if isinstance(part, list) and len(part) > 2:
|
| 870 |
# part[2] might be a string that needs parsing
|
| 871 |
if isinstance(part[2], str):
|
|
|
|
| 881 |
if main_part and isinstance(main_part, list) and len(main_part) > 4 and main_part[4]:
|
| 882 |
body = main_part
|
| 883 |
body_index = part_index
|
| 884 |
+
except (IndexError, TypeError, json.JSONDecodeError, AttributeError) as e:
|
| 885 |
+
# console.log(f"[yellow]Debug: Error parsing part {part_index}: {e}[/yellow]")
|
| 886 |
continue
|
| 887 |
|
| 888 |
# If we found a body, stop looking
|