fix(api): harden toError against circular-reference payloads#146
fix(api): harden toError against circular-reference payloads#146
Conversation
toError in src/api/client.ts wraps JSON.stringify in try/catch and falls back to String(err) when the rejected payload contains a circular reference. Previously a circular err could throw TypeError: Converting circular structure to JSON from inside tauriInvoke catch block, masking the original failure with a normalization error. Three regression tests cover circular objects, plain-object stringification, and string-rejection wrapping.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (3)
📝 WalkthroughWalkthroughThe ChangesError Handling Robustness
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Review rate limit: 4/5 reviews remaining, refill in 12 minutes. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/api/client.ts`:
- Around line 7-10: The current error-wrapping code returns new
Error(JSON.stringify(err)) but doesn't handle JSON.stringify returning undefined
(e.g., for undefined, symbols, or functions), producing a blank Error.message;
update the block in src/api/client.ts that handles err so you first call const
msg = JSON.stringify(err); if msg === undefined then use String(err) (or
fallback like Object.prototype.toString.call(err)) and finally return new
Error(msgOrFallback) while keeping the existing catch fallback for thrown
stringify errors.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: cc9e6104-f15f-4666-8793-daf34d622df3
📒 Files selected for processing (3)
CHANGELOG.mdsrc/api/__tests__/client.test.tssrc/api/client.ts
There was a problem hiding this comment.
1 issue found across 3 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/api/client.ts">
<violation number="1" location="src/api/client.ts:8">
P3: Handle the `JSON.stringify` non-throwing `undefined` case before constructing the `Error`; otherwise some rejected payloads produce an empty error message.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
JSON.stringify returns undefined (not a string) for bare undefined, functions, and symbols. The previous fix only guarded against the throw path; an undefined return would still construct new Error(undefined), producing a blank Error.message and masking the original failure. Coalesce with ?? String(err) so the fallback path is shared with the catch branch. Adds two regression tests covering the bare undefined rejection and the function/symbol rejection paths. Addresses PR #146 review feedback from coderabbitai and cubic-dev-ai.
Summary
Hardens the
toErrorerror-normalization helper insrc/api/client.tsagainst circular-reference payloads. Previously, a circular object rejected byinvokewould throwTypeError: Converting circular structure to JSONfrom inside thetauriInvokecatch block, masking the original IPC failure. Closes #142.Why
When a Rust error from Tauri IPC is enriched or re-wrapped in a way that creates a circular reference before rejection,
JSON.stringify(err)synchronously throws aTypeErrorinstead of succeeding. The uncaught throw occurs inside thetauriInvokecatch handler, replacing the original error with a normalization failure — unhelpful for debugging.Changes
JSON.stringify(err)intry/catchintoError()function.String(err)when JSON serialization fails (covers circular refs and other stringify edge cases).CHANGELOG.mdunder[Unreleased] > Fixedscopeapi.Testing
All TS linting and formatting pass.
oxlintreports 0 warnings.Related Issues
Checklist
Summary by cubic
Hardened
toErrorto handle circular and non-serializable payloads sotauriInvokesurfaces the real IPC error instead of JSON errors or blank messages. Closes #142.JSON.stringify(err)and fall back toString(err); coalesce undefined stringify results to avoid blankError.message.undefined, and function/symbol rejections.Written for commit 18ec66f. Summary will update on new commits.
Summary by CodeRabbit
Bug Fixes
Tests