fix: safe JSON parsing in generateChatResponse#253
Conversation
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ 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 (1)
✨ 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 |
Summary
Closes #170
generateChatResponse()used unchecked casts (as List<dynamic>) and deep indexing (candidates[0]['content']['parts'][0]['text']) on Gemini API responses. Any deviation from the expected structure (rate limit, 500 error returning HTML, empty candidates, missing fields) would causeTypeError,RangeError, orNoSuchMethodErrorcrashes.Changes
lib/services/ai_service.dart— both the main chat block and the function-execution block:jsonDecodewrapped in try/catch — returns controlled error if body isn't valid JSONcandidatesRaw is! List || isEmptyguard before indexingcontentRaw is! Mapguard before accessing['parts']partsRaw is! Listguard before iteratingpart is Mapcheck beforecontainsKey('functionCall')?.toString() ?? ''Before / After