Repository navigation
fix(chat): surface stream error events instead of swallowing them - #251
Conversation
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.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | -11 |
| Duplication | 0 |
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.
| metadata = meta; | ||
| paint(); | ||
| }, | ||
| onError: err => { |
There was a problem hiding this comment.
🔥 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:
| onError: err => { | |
| throw err instanceof Error ? err : new Error(message, { cause: err }); |
📏 Severity: suggestion
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| const api = createApiClient({ baseUrl: settings.backendUrl }); |
There was a problem hiding this comment.
🔥 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
| throw new Error(message); | ||
| }); | ||
| const onDelta = vi.fn(); | ||
| vi.stubGlobal( |
There was a problem hiding this comment.
🔥 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:
| vi.stubGlobal( | |
| vi.spyOn(global, 'fetch').mockResolvedValue( |
📏 Severity: nitpick
Bug: the main chat stream parsed each
data:line and handleddata.errorinside the sametry/catchthat ignored JSON parse failures. A server error event such asUpstream model returned invalid JSONwas logged as a parse warning and never shown.createApiClient().streamChathad the same catch aroundhandleData, so throwing fromonErrorwas also swallowed.Fix: parse JSON in the inner catch only, then call
handleData. The chat UI now usesstreamChatand turnsonErrorinto the existingError: … 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.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.parseandhandleDatalived in the sametry/catchinstreamChat—parse failures and throws fromonErrornever reached the UI.apiClient.jsnow parsesdata:lines in an inner catch only, then invokeshandleData, so valid error payloads reliably triggeronError.App.jsxdrops the inlined SSE/fetchloop for chat and usescreateApiClient().streamChatwithonDelta,onMetadata, andonErrorthat rethrows into the existingError: … Please try again.assistant bubble; streaming latency metadata behavior is preserved via a sharedpaint()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.