fix: harden client JSON parsing and non-blocking campaign bootstrap#330
fix: harden client JSON parsing and non-blocking campaign bootstrap#330
Conversation
Wraps the response.json() call in a try/catch so that malformed JSON responses surface as a clear error instead of an unhandled rejection. Closes #323 https://claude.ai/code/session_011JYS1SQntCzLggHQTrVMHL
…ign timeout - use-auth.ts: switch from try/catch to .catch() pattern matching other hooks - server/index.ts: log campaign bootstrap errors after timeout instead of silently swallowing https://claude.ai/code/session_011JYS1SQntCzLggHQTrVMHL
|
Warning Rate limit exceeded
Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 18 minutes and 9 seconds. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughAdded Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related issues
Possibly related PRs
Suggested labels
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Prevents spurious diffs on future edits to the file. https://claude.ai/code/session_011JYS1SQntCzLggHQTrVMHL
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@client/src/hooks/use-auth.test.ts`:
- Around line 84-101: Update the "exposes isError when response body is not
valid JSON" test for useAuth to also assert the auth state after the parse
failure: after waiting for isLoading to be false, add explicit expectations that
result.current.user is undefined (or null per the hook's contract) and
result.current.isAuthenticated is false, in addition to the existing isError and
error.message checks; modify the test that uses server.use(HttpResponse(...))
and renderHook(() => useAuth()) to include these extra assertions so consumers
can safely branch on auth state after malformed JSON.
In `@server/index.ts`:
- Around line 243-246: The catch handler on campaignPromise currently logs
"failed after timeout" regardless of which promise settled; to fix, add a flag
(e.g., timeoutFired or timeoutWon) that is set when the timeout promise
resolves/wins the race, and only log the "[Bootstrap] Welcome campaign failed
after timeout:" message inside campaignPromise.catch if that flag is true
(otherwise either skip that message or log a different message). Update the code
paths that create the timeout promise (the Promise.race logic) to set
timeoutFired when the timeout resolves and reference that flag in the
campaignPromise.catch so the message reflects whether the timeout actually
fired.
🪄 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: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 315ffcfa-d5bb-4a6b-8b0f-203721d2c2ee
📒 Files selected for processing (3)
client/src/hooks/use-auth.test.tsclient/src/hooks/use-auth.tsserver/index.ts
- use-auth.test.ts: add user/isAuthenticated assertions to malformed-JSON test - server/index.ts: track timedOut flag so campaignPromise.catch only logs when the timeout actually fired, avoiding misleading "failed after timeout" messages when the campaign fails before the timeout https://claude.ai/code/session_011JYS1SQntCzLggHQTrVMHL
Summary
Hardens all client-side React Query hooks against non-JSON server responses (e.g., proxy HTML on 200) by wrapping every
res.json()call with.catch(). Also moves the welcome campaign bootstrap out ofregisterRoutes()intoserver/index.tsas a non-blocking fire-and-forget with a 5-second timeout, preventing cold start delays. Switches the test environment from happy-dom to jsdom for Replit CI compatibility.Changes
Client — JSON parse hardening
response.json()with.catch(), exposeisErroranderrorfromuseQueryres.json()calls on success paths; add.catch(() => ({}))on error paths foruseCreateMonitor,useUpdateMonitor,useCheckMonitorSilent,useUpdateMonitorSilentres.json()calls; guard 3 error-pathres.json()callsres.json()callsres.json()callsres.json()callsres.json()callsres.json()callsres.json()call in sharedfetchJsonhelperServer — startup sequencing
{ httpServer, campaignConfigsReady }instead of barehttpServer; remove campaign bootstrap fromregisterRoutes()Promise.race()and 5s timeout; errors logged to console andErrorLogger; post-timeout outcomes logged instead of silently swallowedTesting
@vitest-environment happy-domto@vitest-environment jsdomhappy-domfrom devDependenciesHow to test
npm run check— TypeScript compiles cleanlynpm run test— All 2076 tests pass (83 test files)npm run build— Production build succeedsCloses #323
https://claude.ai/code/session_011JYS1SQntCzLggHQTrVMHL
Summary by CodeRabbit