Throw Dart errors when calling a method on an unsupported platform#23
Throw Dart errors when calling a method on an unsupported platform#23EchoEllet wants to merge 1 commit into
Conversation
Replaced Exception with UnsupportedError for web checks.
📝 WalkthroughWalkthroughThis change consolidates error handling for web-incompatible APIs by introducing a shared Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes 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 |
There was a problem hiding this comment.
Code Review
This pull request refactors error handling in the flutter_window_close library by replacing generic Exception throws with UnsupportedError and introducing a helper method for web-specific errors. The review feedback suggests further improving consistency by introducing a similar helper for non-web platform errors to ensure uniform error messaging across the API.
| static MethodChannel? _notificationChannel; | ||
| static const MethodChannel _channel = MethodChannel('flutter_window_close'); | ||
|
|
||
| static UnsupportedError _webUnsupported() => UnsupportedError('This method must not be called on the web.'); |
There was a problem hiding this comment.
To maintain consistency and provide a uniform way of handling platform-specific errors, consider adding a helper for non-web unsupported errors as well. This ensures that all platform-specific checks follow the same pattern and use consistent phrasing across the library.
| static UnsupportedError _webUnsupported() => UnsupportedError('This method must not be called on the web.'); | |
| static UnsupportedError _webUnsupported() => UnsupportedError('This method must not be called on the web.'); | |
| static UnsupportedError _nonWebUnsupported() => UnsupportedError('This method must not be called on non-web platforms.'); |
There was a problem hiding this comment.
Currently, there is only one method that is web-only, so creating this utility is not needed.
The purpose of the introduced _webUnsupported() is to be more DRY.
_nonWebUnsupported()
What problem is this trying to solve?
| /// when your app is running in Flutter Web. | ||
| static Future<void> setWebReturnValue(String? returnValue) async { | ||
| if (!kIsWeb) throw Exception('The method only works in Flutter Web.'); | ||
| if (!kIsWeb) throw UnsupportedError('The method must not be called on non-web platforms.'); |
There was a problem hiding this comment.
Use the suggested _nonWebUnsupported() helper here for consistency with the other platform-specific checks in this class. This also aligns the error message phrasing ("This method" vs "The method") with the rest of the API.
| if (!kIsWeb) throw UnsupportedError('The method must not be called on non-web platforms.'); | |
| if (!kIsWeb) throw _nonWebUnsupported(); |
Some methods are not intended to be called on the web, at least according to the doc comments:
So the methods delegate responsibility for checking whether this is a web platform to the consumer (by design). Meaning that if the programmer called
closeWindow()without checking the web platform, it's considered a programming error, not a recoverable failure.In Dart at least,
Errors are not intended to be caught; they are programming bugs that should be addressed rather than handled.The Dart documentation also says:
To provide enhanced semantic and work with tools like Sentry, Firebase Crashlytics, the code should throw an
Errorsuch asUnsupportedError, to indicate a bug (e.g., callingdestroyWindow()on all platforms without checking for web viakIsWebconstant).Related Dart lints: (both are applied in flutter/packages repo)
Summary by CodeRabbit