Skip to content

fix: prevent null reference reading 'error' in MessagesBoundary#1284

Open
wsolarq11 wants to merge 1 commit into
claude-code-best:mainfrom
wsolarq11:fix/messages-boundary-null-error
Open

fix: prevent null reference reading 'error' in MessagesBoundary#1284
wsolarq11 wants to merge 1 commit into
claude-code-best:mainfrom
wsolarq11:fix/messages-boundary-null-error

Conversation

@wsolarq11

@wsolarq11 wsolarq11 commented Jun 24, 2026

Copy link
Copy Markdown

Summary

  • Fixes "Cannot read properties of null (reading 'error')" crash in MessagesBoundary
  • The error boundary catches this during React render when normalizeMessages processes a message array containing null elements

Root Cause

normalizeMessages is called inside useMemo at src/components/Messages.tsx:410, which executes synchronously during React render phase. When the messages array contains a null element (possible via JSONL deserialization, streaming race conditions, or compact/merge operations), flatMap passes it to the callback where message.error is accessed → null.errorTypeError: Cannot read properties of null (reading 'error').

Changes

File Change
src/utils/messages.ts:756 Add if (!message) return [] null guard in normalizeMessages flatMap
src/utils/messages.ts:781 error: message.errorerror: message?.error
src/utils/messages.ts:2380 Add if (!message) return [] null guard in normalizeMessagesForAPI forEach
src/utils/messages/mappers.ts:32 Add if (!message) return [] null guard in toInternalMessages flatMap
src/utils/messages/mappers.ts:131 Add if (!message) return [] null guard in fromSDKCompactMetadata flatMap
src/utils/messages/mappers.ts:143 error: message.errorerror: message?.error

Verification

  • All changes are purely defensive — no behavior change for non-null messages
  • Optional chaining and null guards are standard TypeScript patterns
  • Indentation verified at all 4 guard locations (4-space and 6-space matching surrounding code)

🤖 Generated with Claude Code Best

Summary by CodeRabbit

  • Bug Fixes
    • Improved message handling to safely skip empty or missing entries during normalization and conversion.
    • Prevented runtime errors when assistant message error details are unavailable.
    • Made message-processing flows more resilient when input data contains unexpected null or undefined values.

normalizeMessages and toSDKMessages access message.error in flatMap
callbacks executed during React render (via useMemo). When a null
element is present in the messages array at runtime (e.g., from
corrupted JSONL deserialization or streaming race conditions),
null.error throws "Cannot read properties of null (reading 'error')".

Add null guards to flatMap/forEach callbacks and optional chaining
on message.error access points in normalizeMessages and
toSDKMessages.

Fixes: React Rendering Error in MessagesBoundary

Co-Authored-By: deepseek-v4-pro[1m] <deepseek-ai@claude-code-best.win>
@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 26b01b5c-697c-4ac6-bf36-e9ef7395d0a3

📥 Commits

Reviewing files that changed from the base of the PR and between d3121f0 and 2c80150.

📒 Files selected for processing (2)
  • src/utils/messages.ts
  • src/utils/messages/mappers.ts

📝 Walkthrough

Walkthrough

Adds falsy-entry guards (if (!message) return []) to normalizeMessages, normalizeMessagesForAPI, toInternalMessages, and toSDKMessages to prevent property access on undefined array elements. Also changes message.error to message?.error in the assistant conversion paths of both files.

Changes

Null Guards for Message Array Entries

Layer / File(s) Summary
Falsy-entry guards and null-safe error access
src/utils/messages.ts, src/utils/messages/mappers.ts
normalizeMessages, normalizeMessagesForAPI, toInternalMessages, and toSDKMessages each short-circuit on falsy message items before accessing message.type; the error field in the assistant branch of both files is read with optional chaining (message?.error).

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐇 A message arrived, but was empty and bare,
The array held nothing — just undefined air.
With a guard and a ?. placed just right,
No crash shall occur in the deep of the night.
Hoppity-safe! No more undefined fright. 🌙

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main fix: preventing null-related crashes when reading message error fields in MessagesBoundary.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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