From e6931e68882aa73b5b4e6163d0db890f76dc0955 Mon Sep 17 00:00:00 2001 From: Abner <22141172+Silentely@users.noreply.github.com> Date: Mon, 1 Dec 2025 11:39:12 +0800 Subject: [PATCH] Enhance error handling for JSON decoding (#24) Improve error handling for non-JSON lines by printing to stderr instead of breaking the flow. --- src/codexmcp/server.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/codexmcp/server.py b/src/codexmcp/server.py index 73a1e3b..c0a1bfa 100644 --- a/src/codexmcp/server.py +++ b/src/codexmcp/server.py @@ -221,15 +221,18 @@ async def codex( err_message = "codex error: " + line_dict.get("error", {}).get("message", "") if "error" in line_dict.get("type", ""): error_msg = line_dict.get("message", "") + import re is_reconnecting = bool(re.match(r'^Reconnecting\.\.\.\s+\d+/\d+$', error_msg)) + if not is_reconnecting: success = False if len(agent_messages) == 0 else success err_message = "codex error: " + error_msg - except json.JSONDecodeError as error: - # Improved error handling: include problematic line - err_message = line - success = False - break + + except json.JSONDecodeError: + import sys + print(f"Ignored non-JSON line: {line}", file=sys.stderr) + continue + except Exception as error: err_message = f"Unexpected error: {error}. Line: {line!r}" success = False