fix: map PlatformException to proper ErrorCode in all methods#621
fix: map PlatformException to proper ErrorCode in all methods#621
Conversation
PlatformException from native iOS/Android was being caught generically and always mapped to ErrorCode.ServiceError, losing the actual error code (e.g., user-cancelled). Now uses ErrorCodeUtils.fromPlatformCode() to preserve the correct error code across 11 methods. Closes #619 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review infoConfiguration used: defaults Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughIntroduces a centralized PlatformException→PurchaseError translator and applies it across initialization, connection, purchase, product, storefront, and subscription flows; adds unit tests updating mocked platform error codes and new PurchaseHelpers tests; documentation adds Codecov guidance. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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 |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the error handling within the in-app purchase plugin by ensuring that native platform exceptions are accurately translated into specific Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a significant improvement in error handling by consistently mapping PlatformExceptions to specific ErrorCodes. The new _purchaseErrorFromPlatformException helper centralizes this logic, enhancing maintainability. The accompanying test additions and updates are thorough and ensure the new functionality is well-covered. The changes are well-implemented, but I've noted one minor inconsistency in an error handling pattern that could be aligned with the rest of the changes for better code consistency.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
lib/flutter_inapp_purchase.dart (1)
1722-1737: Preserve mapped platform on the emittedPurchaseError.Line 1732 builds
PurchaseErrorwithout settingplatform, even though platform is already determined for code mapping. Passing it explicitly keeps error metadata consistent with the mapped code.♻️ Proposed tweak
PurchaseError _purchaseErrorFromPlatformException( PlatformException error, String operation, ) { - final errorCode = errors.ErrorCodeUtils.fromPlatformCode( - error.code, - _platform.isIOS || _platform.isMacOS - ? gentype.IapPlatform.IOS - : gentype.IapPlatform.Android, - ); + final platform = _platform.isIOS || _platform.isMacOS + ? gentype.IapPlatform.IOS + : gentype.IapPlatform.Android; + final errorCode = errors.ErrorCodeUtils.fromPlatformCode( + error.code, + platform, + ); return PurchaseError( code: errorCode, + platform: platform, message: 'Failed to $operation [${error.code}]: ' '${error.message ?? error.details}', ); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/flutter_inapp_purchase.dart` around lines 1722 - 1737, In _purchaseErrorFromPlatformException, you determine the mapped platform for error codes but don’t set it on the returned PurchaseError; compute and store the chosen platform (the gentype.IapPlatform value used in errors.ErrorCodeUtils.fromPlatformCode) in a local variable and pass it into the PurchaseError constructor as the platform field so the emitted PurchaseError includes the same platform metadata as the mapped error code.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@lib/flutter_inapp_purchase.dart`:
- Around line 1722-1737: In _purchaseErrorFromPlatformException, you determine
the mapped platform for error codes but don’t set it on the returned
PurchaseError; compute and store the chosen platform (the gentype.IapPlatform
value used in errors.ErrorCodeUtils.fromPlatformCode) in a local variable and
pass it into the PurchaseError constructor as the platform field so the emitted
PurchaseError includes the same platform metadata as the mapped error code.
ℹ️ Review info
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
lib/flutter_inapp_purchase.darttest/errors_unit_test.darttest/flutter_inapp_purchase_channel_test.darttest/ios_methods_test.dart
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #621 +/- ##
==========================================
+ Coverage 68.11% 69.94% +1.83%
==========================================
Files 7 7
Lines 1521 1554 +33
==========================================
+ Hits 1036 1087 +51
+ Misses 485 467 -18 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
- Add platform field to PurchaseError from _purchaseErrorFromPlatformException - Unify getActiveSubscriptions to use on PlatformException catch pattern - Add tests for endConnection, getStorefront, getStorefrontIOS, presentCodeRedemptionSheetIOS, showManageSubscriptionsIOS, getActiveSubscriptions, isEligibleForExternalPurchaseCustomLinkIOS - Add test verifying platform field is set on mapped errors Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
🧹 Nitpick comments (1)
lib/flutter_inapp_purchase.dart (1)
1721-1739: Consider usinggetCurrentPlatform()for platform detection.The helper method correctly maps PlatformException to PurchaseError using
ErrorCodeUtils.fromPlatformCode. However, based on learnings, prefer usingerrors.getCurrentPlatform()over inline platform conditionals for consistent runtime platform reporting in error handling.♻️ Suggested refactor
PurchaseError _purchaseErrorFromPlatformException( PlatformException error, String operation, ) { - final platform = _platform.isIOS || _platform.isMacOS - ? gentype.IapPlatform.IOS - : gentype.IapPlatform.Android; + final platform = errors.getCurrentPlatform(); final errorCode = errors.ErrorCodeUtils.fromPlatformCode( error.code, platform, );Based on learnings: "In flutter_inapp_purchase project, prefer using iap_err.getCurrentPlatform() in error handling over hardcoded platform constants to ensure accurate runtime platform reporting in error messages and debugging."
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@lib/flutter_inapp_purchase.dart` around lines 1721 - 1739, In _purchaseErrorFromPlatformException replace the inline platform detection (_platform.isIOS || _platform.isMacOS ? gentype.IapPlatform.IOS : gentype.IapPlatform.Android) with the canonical runtime helper errors.getCurrentPlatform() (or the appropriate getCurrentPlatform() exported by the errors/iap_err module) and pass that platform into errors.ErrorCodeUtils.fromPlatformCode and the PurchaseError.platform field so runtime platform reporting is consistent for ErrorCodeUtils.fromPlatformCode and the returned PurchaseError.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@lib/flutter_inapp_purchase.dart`:
- Around line 1721-1739: In _purchaseErrorFromPlatformException replace the
inline platform detection (_platform.isIOS || _platform.isMacOS ?
gentype.IapPlatform.IOS : gentype.IapPlatform.Android) with the canonical
runtime helper errors.getCurrentPlatform() (or the appropriate
getCurrentPlatform() exported by the errors/iap_err module) and pass that
platform into errors.ErrorCodeUtils.fromPlatformCode and the
PurchaseError.platform field so runtime platform reporting is consistent for
ErrorCodeUtils.fromPlatformCode and the returned PurchaseError.
ℹ️ Review info
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
lib/flutter_inapp_purchase.darttest/errors_unit_test.dart
🚧 Files skipped from review as they are similar to previous changes (1)
- test/errors_unit_test.dart
- Add 20 tests for PurchaseHelpers extension (0% -> 100% coverage) - Add codecov management section to CLAUDE.md Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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 `@CLAUDE.md`:
- Around line 101-106: The fenced code block in the numbered list item is
missing blank lines above and below (MD031); edit the CLAUDE.md list item
containing the fenced block with the two commands (flutter test --coverage and
dart run tool/filter_coverage.dart) to insert an empty line before the opening
``` and an empty line after the closing ``` so the block is surrounded by blank
lines.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary
PlatformExceptionerror codes from native iOS/Android to correctErrorCodevalues using existingErrorCodeUtils.fromPlatformCode()infrastructurePlatformExceptionerrors were hardcoded toErrorCode.ServiceError, losing the actual error code (e.g.,user-cancelled->UserCancelled)_purchaseErrorFromPlatformExceptionhelper and updated 11 methodsChanges
Dart Library (lib/)
_purchaseErrorFromPlatformExceptionhelper methodinitConnection,endConnection,requestPurchase,getAvailablePurchases,getStorefront,getStorefrontIOS,presentCodeRedemptionSheetIOS,showManageSubscriptionsIOS,_fetchProductsInternal,getActiveSubscriptions,isEligibleForExternalPurchaseCustomLinkIOSTests (test/)
'platform'/'500'Closes #619
Test plan
dart format --page-width 80 --set-exit-if-changedpassesflutter analyzepassesflutter testpasses (213 tests)Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Tests
Documentation