Skip to content

Throw Dart errors when calling a method on an unsupported platform#23

Open
EchoEllet wants to merge 1 commit into
zonble:mainfrom
EchoEllet:patch-1
Open

Throw Dart errors when calling a method on an unsupported platform#23
EchoEllet wants to merge 1 commit into
zonble:mainfrom
EchoEllet:patch-1

Conversation

@EchoEllet
Copy link
Copy Markdown

@EchoEllet EchoEllet commented Apr 30, 2026

Some methods are not intended to be called on the web, at least according to the doc comments:

/// ...
/// - The method does not support Flutter Web.
static Future<void> closeWindow()

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:

Errors differ from Exceptions in that Errors can be analyzed and prevented prior to runtime. It should almost never be necessary to catch an error at runtime.

To provide enhanced semantic and work with tools like Sentry, Firebase Crashlytics, the code should throw an Error such as UnsupportedError, to indicate a bug (e.g., calling destroyWindow() on all platforms without checking for web viakIsWeb constant).

Related Dart lints: (both are applied in flutter/packages repo)

Summary by CodeRabbit

  • Refactor
    • Unified error handling for unsupported window operations by introducing a shared error handler and replacing generic exceptions with explicit, descriptive error messages for platform-incompatible API usage.

Replaced Exception with UnsupportedError for web checks.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 30, 2026

📝 Walkthrough

Walkthrough

This change consolidates error handling for web-incompatible APIs by introducing a shared _webUnsupported() helper function and updating existing methods to throw UnsupportedError instead of generic Exception types for improved clarity.

Changes

Cohort / File(s) Summary
Error Handling Consolidation
lib/flutter_window_close.dart
Introduces shared _webUnsupported() helper function that throws UnsupportedError for web-incompatible APIs. Updates setWindowShouldCloseHandler, closeWindow, destroyWindow, and setWebReturnValue to use this helper or throw UnsupportedError instead of generic Exception.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A rabbit hops through error trails,
Consolidating thrown details,
UnsupportedError rings so clear,
No web-platform fears right here! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: converting platform-unsupported method calls to throw Dart errors instead of generic exceptions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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.');

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
if (!kIsWeb) throw UnsupportedError('The method must not be called on non-web platforms.');
if (!kIsWeb) throw _nonWebUnsupported();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant