Skip to content

fix(chat): surface stream error events instead of swallowing them - #251

Merged
mangeshraut712 merged 1 commit into
mainfrom
fix/chat-stream-error-swallowed
Nov 25, 2026
Merged

mangeshraut712 merged 1 commit into
mainfrom
fix/chat-stream-error-swallowed

Conversation

@mangeshraut712

@mangeshraut712 mangeshraut712 commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Bug: the main chat stream parsed each data: line and handled data.error inside the same try/catch that ignored JSON parse failures. A server error event such as Upstream model returned invalid JSON was logged as a parse warning and never shown. createApiClient().streamChat had the same catch around handleData, so throwing from onError was also swallowed.

Fix: parse JSON in the inner catch only, then call handleData. The chat UI now uses streamChat and turns onError into the existing Error: … Please try again. bubble.

Test: surfaces server error events even when the message mentions JSON — the promise resolved on the old catch and now rejects with the server message. Frontend: 85 tests passed.


Devin Review


Note

Low Risk
Localized chat streaming and client parsing fix with a regression test; no auth or persistence changes.

Overview
Fixes a bug where server stream error events (including messages containing the word “JSON”) were swallowed because JSON.parse and handleData lived in the same try/catch in streamChat—parse failures and throws from onError never reached the UI.

apiClient.js now parses data: lines in an inner catch only, then invokes handleData, so valid error payloads reliably trigger onError.

App.jsx drops the inlined SSE/fetch loop for chat and uses createApiClient().streamChat with onDelta, onMetadata, and onError that rethrows into the existing Error: … Please try again. assistant bubble; streaming latency metadata behavior is preserved via a shared paint() helper.

Adds a test that the stream rejects with the server error text after partial content when the error message mentions JSON.

Reviewed by Cursor Bugbot for commit ced6a9e. Bugbot is set up for automated code reviews on this repo. Configure here.

The chat SSE parser caught JSON.parse failures and handler throws in the
same empty catch, so a server error event never reached the UI. Parse
only the payload in that catch, let onError propagate, and route the
main chat through streamChat. A regression test fails when the throw is
swallowed, including when the error message mentions JSON.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T14:55:29.898835Z ced6a9e PR opened
🔒 Security Review ✅ Completed 2026-11-25T14:56:54.412351Z ced6a9e PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics -11 complexity · 0 duplication

Metric Results
Complexity -11
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@mangeshraut712
mangeshraut712 merged commit 703411a into main Nov 25, 2026
11 of 12 checks passed
@mangeshraut712
mangeshraut712 deleted the fix/chat-stream-error-swallowed branch November 25, 2026 15:01
Comment thread src/App.jsx
metadata = meta;
paint();
},
onError: err => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 The Roast: You're throwing a brand new Error here, which means the original error's stack trace gets yeeted into the void. When this hits production at 3 AM, you'll be debugging with a stack trace that points to this line instead of where the actual failure occurred. Error.cause exists for a reason — use it.

🩹 The Fix:

Suggested change
onError: err => {
throw err instanceof Error ? err : new Error(message, { cause: err });

📏 Severity: suggestion

Comment thread src/App.jsx
method: 'POST',
headers: {
'Content-Type': 'application/json',
const api = createApiClient({ baseUrl: settings.backendUrl });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 The Roast: streamChat accepts a signal parameter for AbortController integration, but you're not passing one. If the user navigates away mid-stream, the fetch keeps running, the callbacks keep firing, and React screams about state updates on unmounted components. This is the "can't read property of null" bug waiting to happen.

🩹 The Fix: Wire up an AbortController in streamAssistantReply and pass its signal. Clean up in a finally block or use an effect with a cleanup function.

📏 Severity: warning

Comment thread src/lib/apiClient.test.js
throw new Error(message);
});
const onDelta = vi.fn();
vi.stubGlobal(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 The Roast: vi.stubGlobal('fetch', ...) is deprecated in Vitest and will warn (or break) in future versions. The modern way is vi.spyOn(global, 'fetch') which also restores automatically.

🩹 The Fix:

Suggested change
vi.stubGlobal(
vi.spyOn(global, 'fetch').mockResolvedValue(

📏 Severity: nitpick

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant