Skip to content

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

Merged
claude-code-best merged 3 commits into
claude-code-best:mainfrom
wsolarq11:fix/messages-boundary-null-error
Jun 25, 2026
Merged

fix: prevent null reference reading 'error' in MessagesBoundary#1284
claude-code-best merged 3 commits into
claude-code-best:mainfrom
wsolarq11:fix/messages-boundary-null-error

Conversation

@wsolarq11

@wsolarq11 wsolarq11 commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

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 normalization and conversion to safely skip empty or missing message entries.
    • Prevented runtime errors by handling assistant message error details in a null-safe way.
    • Made message-processing more resilient when inputs contain unexpected null/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: 63446832-66a2-4e03-94e0-93bbf840e1e3

📥 Commits

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

📒 Files selected for processing (2)
  • src/utils/messages.ts
  • src/utils/messages/mappers.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/utils/messages/mappers.ts
  • src/utils/messages.ts

📝 Walkthrough

Walkthrough

Adds falsy-entry guards to message normalization and mapping helpers, and makes assistant error-field access null-safe in both paths.

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 drifted in, but none was there,
A tiny guard caught empty air.
With ?. soft and checks so kind,
No brittle crash shall now unwind.

🚥 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 reference access to error during message normalization/rendering.
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.

@claude-code-best

Copy link
Copy Markdown
Owner

@wsolarq11 先 reset 一个 commit, 然后把 main 合入你的分支, 不是你的错

wsolarq11 and others added 2 commits June 25, 2026 14:50
Co-Authored-By: deepseek-v4-pro[1m] <deepseek-ai@claude-code-best.win>
@wsolarq11 wsolarq11 force-pushed the fix/messages-boundary-null-error branch from f0dde83 to c094516 Compare June 25, 2026 06:51
@claude-code-best claude-code-best merged commit 0753daf into claude-code-best:main Jun 25, 2026
2 checks passed
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.

2 participants