Repository navigation
fix: reapply automatic error hooks on repeated setup #629
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
da957db
fix: preserve chained error handlers across restarts
marandaneto 5517b70
fix: reapply automatic error hooks on repeated setup
marandaneto 14b25c7
test: cover isolate hooks across repeated setup
marandaneto 3c8c3d3
Merge remote-tracking branch 'origin/main' into fix/repeated-setup-hooks
marandaneto 87ffa7c
fix: correct changeset package after merging main
marandaneto 426feee
docs: clarify settings reapplied by repeated setup
marandaneto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'posthog_flutter': patch | ||
| --- | ||
|
|
||
| Reapply Dart automatic error-capture hooks when setup is called again with changed flags. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| import 'dart:ui'; | ||
|
|
||
| import 'package:flutter/foundation.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:posthog_flutter/posthog_flutter.dart'; | ||
| import 'package:posthog_flutter/src/posthog_flutter_platform_interface.dart'; | ||
|
|
||
| import 'posthog_flutter_platform_interface_fake.dart'; | ||
|
|
||
| void main() { | ||
| TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
|
||
| late PosthogFlutterPlatformFake platform; | ||
| FlutterExceptionHandler? originalFlutterHandler; | ||
| ErrorCallback? originalPlatformHandler; | ||
|
|
||
| setUp(() async { | ||
| platform = PosthogFlutterPlatformFake(); | ||
| PosthogFlutterPlatformInterface.instance = platform; | ||
| await Posthog().close(); | ||
| originalFlutterHandler = FlutterError.onError; | ||
| FlutterError.onError = (_) {}; | ||
| if (!kIsWeb) { | ||
| originalPlatformHandler = PlatformDispatcher.instance.onError; | ||
| PlatformDispatcher.instance.onError = (_, __) => true; | ||
| } | ||
| }); | ||
|
|
||
| tearDown(() async { | ||
| await Posthog().close(); | ||
| FlutterError.onError = originalFlutterHandler; | ||
| if (!kIsWeb) { | ||
| PlatformDispatcher.instance.onError = originalPlatformHandler; | ||
| } | ||
| }); | ||
|
|
||
| void reportFlutterError({bool silent = false}) { | ||
| FlutterError.reportError(FlutterErrorDetails( | ||
| exception: StateError('repeated setup repro'), | ||
| stack: StackTrace.current, | ||
| silent: silent, | ||
| )); | ||
| } | ||
|
|
||
| for (final reuseConfig in [false, true]) { | ||
| test('disables Flutter hook on repeated setup (reuse=$reuseConfig)', | ||
| () async { | ||
| final upstream = FlutterError.onError; | ||
| final first = PostHogConfig('test-token') | ||
| ..errorTrackingConfig.captureFlutterErrors = true; | ||
| await Posthog().setup(first); | ||
| reportFlutterError(); | ||
| expect(platform.capturedExceptions, hasLength(1)); | ||
| platform.capturedExceptions.clear(); | ||
|
|
||
| final second = reuseConfig ? first : PostHogConfig('test-token'); | ||
| second.errorTrackingConfig.captureFlutterErrors = false; | ||
| await Posthog().setup(second); | ||
| expect(Posthog().config, same(second)); | ||
|
|
||
| reportFlutterError(); | ||
| expect(platform.capturedExceptions, isEmpty); | ||
| expect(FlutterError.onError, same(upstream)); | ||
| }); | ||
| } | ||
|
|
||
| test('disables PlatformDispatcher hook on repeated setup', () async { | ||
| final upstream = PlatformDispatcher.instance.onError; | ||
| final first = PostHogConfig('test-token') | ||
| ..errorTrackingConfig.capturePlatformDispatcherErrors = true; | ||
| await Posthog().setup(first); | ||
| PlatformDispatcher.instance.onError!( | ||
| StateError('before'), StackTrace.current); | ||
| expect(platform.capturedExceptions, hasLength(1)); | ||
| platform.capturedExceptions.clear(); | ||
|
|
||
| await Posthog().setup(PostHogConfig('test-token')); | ||
| PlatformDispatcher.instance.onError!( | ||
| StateError('after'), StackTrace.current); | ||
| expect(platform.capturedExceptions, isEmpty); | ||
| expect(PlatformDispatcher.instance.onError, same(upstream)); | ||
| }, skip: kIsWeb); | ||
|
|
||
| test('adds PlatformDispatcher hook while Flutter hook is installed', | ||
| () async { | ||
| final first = PostHogConfig('test-token') | ||
| ..errorTrackingConfig.captureFlutterErrors = true; | ||
| await Posthog().setup(first); | ||
|
|
||
| final second = PostHogConfig('test-token') | ||
| ..errorTrackingConfig.captureFlutterErrors = true | ||
| ..errorTrackingConfig.capturePlatformDispatcherErrors = true; | ||
| await Posthog().setup(second); | ||
| expect(Posthog().config, same(second)); | ||
|
|
||
| PlatformDispatcher.instance.onError!( | ||
| StateError('after'), StackTrace.current); | ||
| expect(platform.capturedExceptions, hasLength(1)); | ||
| }, skip: kIsWeb); | ||
|
|
||
| test('replaces silent-error policy on repeated setup', () async { | ||
| final first = PostHogConfig('test-token') | ||
| ..errorTrackingConfig.captureFlutterErrors = true; | ||
| await Posthog().setup(first); | ||
|
|
||
| final second = PostHogConfig('test-token') | ||
| ..errorTrackingConfig.captureFlutterErrors = true | ||
| ..errorTrackingConfig.captureSilentFlutterErrors = true; | ||
| await Posthog().setup(second); | ||
|
|
||
| reportFlutterError(silent: true); | ||
| expect(platform.capturedExceptions, hasLength(1)); | ||
| }); | ||
|
|
||
| test('mutating the original silent-error policy takes effect', () async { | ||
|
marandaneto marked this conversation as resolved.
|
||
| final config = PostHogConfig('test-token') | ||
| ..errorTrackingConfig.captureFlutterErrors = true; | ||
| await Posthog().setup(config); | ||
| config.errorTrackingConfig.captureSilentFlutterErrors = true; | ||
| await Posthog().setup(config); | ||
|
|
||
| reportFlutterError(silent: true); | ||
| expect(platform.capturedExceptions, hasLength(1)); | ||
| }); | ||
|
|
||
| test('first installation can happen on second setup', () async { | ||
| await Posthog().setup(PostHogConfig('test-token')); | ||
| await Posthog().setup(PostHogConfig('test-token') | ||
| ..errorTrackingConfig.captureFlutterErrors = true); | ||
|
|
||
| reportFlutterError(); | ||
| expect(platform.capturedExceptions, hasLength(1)); | ||
| }); | ||
|
|
||
| test('identical repeated setup does not duplicate capture', () async { | ||
| final config = PostHogConfig('test-token') | ||
| ..errorTrackingConfig.captureFlutterErrors = true; | ||
| await Posthog().setup(config); | ||
| await Posthog().setup(config); | ||
|
|
||
| reportFlutterError(); | ||
| expect(platform.capturedExceptions, hasLength(1)); | ||
| }); | ||
|
|
||
| for (final platformHandler in [false, true]) { | ||
| test( | ||
| 'repeated setup preserves wrapped handlers (platform=$platformHandler)', | ||
| () async { | ||
| var upstreamCalls = 0; | ||
| var wrapperCalls = 0; | ||
| final config = PostHogConfig('test-token') | ||
| ..errorTrackingConfig.captureFlutterErrors = !platformHandler | ||
| ..errorTrackingConfig.capturePlatformDispatcherErrors = platformHandler; | ||
|
|
||
| if (platformHandler) { | ||
| PlatformDispatcher.instance.onError = (_, __) { | ||
| upstreamCalls++; | ||
| return true; | ||
| }; | ||
| } else { | ||
| FlutterError.onError = (_) => upstreamCalls++; | ||
| } | ||
| await Posthog().setup(config); | ||
| if (platformHandler) { | ||
| final delegate = PlatformDispatcher.instance.onError!; | ||
| PlatformDispatcher.instance.onError = (error, stack) { | ||
| wrapperCalls++; | ||
| return delegate(error, stack); | ||
| }; | ||
| } else { | ||
| final delegate = FlutterError.onError!; | ||
| FlutterError.onError = (details) { | ||
| wrapperCalls++; | ||
| delegate(details); | ||
| }; | ||
| } | ||
|
|
||
| for (var i = 0; i < 3; i++) { | ||
| await Posthog().setup(config); | ||
| } | ||
| if (platformHandler) { | ||
| expect( | ||
| PlatformDispatcher.instance.onError!( | ||
| StateError('after'), StackTrace.current), | ||
| isTrue, | ||
| ); | ||
| } else { | ||
| reportFlutterError(); | ||
| } | ||
| expect(platform.capturedExceptions, hasLength(1)); | ||
| expect(upstreamCalls, 1); | ||
| expect(wrapperCalls, 1); | ||
| }, skip: kIsWeb && platformHandler); | ||
| } | ||
|
|
||
| test('close then setup applies changed hook flags', () async { | ||
| await Posthog().setup(PostHogConfig('test-token') | ||
| ..errorTrackingConfig.captureFlutterErrors = true); | ||
| await Posthog().close(); | ||
| await Posthog().setup(PostHogConfig('test-token') | ||
| ..errorTrackingConfig.capturePlatformDispatcherErrors = true); | ||
|
|
||
| reportFlutterError(); | ||
| expect(platform.capturedExceptions, isEmpty); | ||
| PlatformDispatcher.instance.onError!( | ||
| StateError('after'), StackTrace.current); | ||
| expect(platform.capturedExceptions, hasLength(1)); | ||
| }, skip: kIsWeb); | ||
| } | ||
59 changes: 59 additions & 0 deletions
59
posthog_flutter/test/repeated_setup_isolate_hooks_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| @TestOn('vm') | ||
| library; | ||
|
|
||
| import 'dart:async'; | ||
| import 'dart:isolate'; | ||
|
|
||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:posthog_flutter/posthog_flutter.dart'; | ||
| import 'package:posthog_flutter/src/posthog_flutter_platform_interface.dart'; | ||
|
|
||
| import 'posthog_flutter_platform_interface_fake.dart'; | ||
|
|
||
| void main() { | ||
| test('repeated setup turns the isolate hook off and back on', () async { | ||
| final results = ReceivePort(); | ||
| final isolate = await Isolate.spawn( | ||
| _exerciseIsolateHook, | ||
| results.sendPort, | ||
| errorsAreFatal: false, | ||
| onExit: results.sendPort, | ||
| ); | ||
| addTearDown(() { | ||
| isolate.kill(priority: Isolate.immediate); | ||
| results.close(); | ||
| }); | ||
|
|
||
| expect(await results.first, [1, 0, 1]); | ||
| }); | ||
| } | ||
|
|
||
| Future<void> _exerciseIsolateHook(SendPort results) async { | ||
| final observedErrors = ReceivePort(); | ||
| final errors = StreamIterator<dynamic>(observedErrors); | ||
| Isolate.current.addErrorListener(observedErrors.sendPort); | ||
| final platform = PosthogFlutterPlatformFake(); | ||
| PosthogFlutterPlatformInterface.instance = platform; | ||
|
|
||
| try { | ||
| final counts = <int>[]; | ||
| for (final enabled in [true, false, true]) { | ||
| await Posthog().setup(PostHogConfig('test-token') | ||
| ..errorTrackingConfig.captureIsolateErrors = enabled); | ||
| platform.capturedExceptions.clear(); | ||
| Timer.run(() => throw StateError('isolate hook enabled=$enabled')); | ||
| await errors.moveNext(); | ||
| // Error listeners use separate ports; let the SDK's queued callback run. | ||
| await Future<void>.delayed(Duration.zero); | ||
| counts.add(platform.capturedExceptions.length); | ||
| } | ||
| results.send(counts); | ||
| } catch (error, stack) { | ||
| results.send('$error\n$stack'); | ||
| } finally { | ||
| await Posthog().close(); | ||
| Isolate.current.removeErrorListener(observedErrors.sendPort); | ||
| await errors.cancel(); | ||
| observedErrors.close(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.