Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[draft] fix(web): don't create runZoneGuarded on web if it already exists #2088

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions flutter/lib/src/sentry_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,23 @@
final platformDispatcher = PlatformDispatcher.instance;
final wrapper = PlatformDispatcherWrapper(platformDispatcher);

// Flutter Web don't capture [Future] errors if using [PlatformDispatcher.onError] and not
// Flutter Web doesn't capture [Future] errors if using [PlatformDispatcher.onError] and not
// the [runZonedGuarded].
// likely due to https://github.com/flutter/flutter/issues/100277
final isOnErrorSupported = flutterOptions.platformChecker.isWeb
? false
: wrapper.isOnErrorSupported(flutterOptions);
final bool isOnErrorSupported = !flutterOptions.platformChecker.isWeb &&
wrapper.isOnErrorSupported(flutterOptions);

final runZonedGuardedOnError = flutterOptions.platformChecker.isWeb
? _createRunZonedGuardedOnError()
final bool customZoneExists = Zone.current != Zone.root;

// If onError is not supported and no custom zone exists, use runZonedGuarded to capture errors.
final bool useRunZonedGuarded = !isOnErrorSupported && !customZoneExists;

// Retrieve the onError callback set by the user if a custom zone exists.
final RunZonedGuardedOnError? additionalOnError =
customZoneExists ? Zone.current.handleUncaughtError : null;

RunZonedGuardedOnError? runZonedGuardedOnError = useRunZonedGuarded
? _createRunZonedGuardedOnError(additionalOnError: additionalOnError)

Check warning on line 91 in flutter/lib/src/sentry_flutter.dart

View check run for this annotation

Codecov / codecov/patch

flutter/lib/src/sentry_flutter.dart#L91

Added line #L91 was not covered by tests
: null;

// first step is to install the native integration and set default values,
Expand All @@ -104,7 +112,7 @@
// ignore: invalid_use_of_internal_member
options: flutterOptions,
// ignore: invalid_use_of_internal_member
callAppRunnerInRunZonedGuarded: !isOnErrorSupported,
callAppRunnerInRunZonedGuarded: useRunZonedGuarded,
// ignore: invalid_use_of_internal_member
runZonedGuardedOnError: runZonedGuardedOnError,
);
Expand Down Expand Up @@ -199,13 +207,19 @@
return integrations;
}

static RunZonedGuardedOnError _createRunZonedGuardedOnError() {
static RunZonedGuardedOnError _createRunZonedGuardedOnError({

Check warning on line 210 in flutter/lib/src/sentry_flutter.dart

View check run for this annotation

Codecov / codecov/patch

flutter/lib/src/sentry_flutter.dart#L210

Added line #L210 was not covered by tests
RunZonedGuardedOnError? additionalOnError,
}) {
return (Object error, StackTrace stackTrace) async {
final errorDetails = FlutterErrorDetails(
exception: error,
stack: stackTrace,
);
FlutterError.dumpErrorToConsole(errorDetails, forceReport: true);

if (additionalOnError != null) {
additionalOnError(error, stackTrace);

Check warning on line 221 in flutter/lib/src/sentry_flutter.dart

View check run for this annotation

Codecov / codecov/patch

flutter/lib/src/sentry_flutter.dart#L221

Added line #L221 was not covered by tests
}
};
}

Expand Down
Loading