-
Notifications
You must be signed in to change notification settings - Fork 53
feat: autocapture unhandled exceptions #214
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
Merged
Merged
Changes from 61 commits
Commits
Show all changes
69 commits
Select commit
Hold shift + click to select a range
592f6e2
feat: add public interface
ioannisj 23f7ba9
feat: add config and exception processor
ioannisj 7f3dc16
feat: native plugins
ioannisj 8b40927
chore: update sample app
ioannisj 7c55c87
feat: add unit tests
ioannisj b0bafda
feat: handle primitives
ioannisj 73f6e1b
chore: update changelog
ioannisj 742f23f
fix: package and module
ioannisj dd88514
feat: add autocapture integration
ioannisj 248ea91
chore: update changelog
ioannisj cf504fb
fix: format
ioannisj e137c1e
fix: format
ioannisj 6fb190d
fix: use Object vs dynamic
ioannisj 7744b92
chore: rename folder
ioannisj e32fee1
fix: make stackTrace optional
ioannisj c35cb81
fix: clean generated stack trace
ioannisj cfe8868
feat: add unit tests
ioannisj 16577ed
fix: make function optional
ioannisj f5ff9c5
fix: drop primitive check
ioannisj 2ef7146
fix: skip module
ioannisj 690bae0
fix: make thread_id optional
ioannisj 1651e5c
fix: update config
ioannisj 585a68e
fix: error type
ioannisj a5f22e4
fix: doc
ioannisj a077460
fix: handle empty stack trace
ioannisj c21b68c
fix: allow overwriting exception properties
ioannisj 19d2c19
fix: normalize props
ioannisj e3d6680
chore: bump min dart and flutter version
ioannisj 084a8b6
fix: generate event timestamp flutter side
ioannisj e5145d9
fix: remove handled from public api
ioannisj 6514ab4
fix: platform call arguments
ioannisj 821f05a
fix: example app
ioannisj 907b8ec
fix: do not try to parse exception package
ioannisj 5e19639
fix: normalize props
ioannisj 298106d
fix: normalize sets
ioannisj bdec329
fix: remove handled from public interface
ioannisj d491713
fix: web handler
ioannisj 59ed147
fix: replace hof with direct iteration
ioannisj 5a6d7d7
fix:
ioannisj 6c0529c
feat: add web support
ioannisj c4babf0
chore: add config comment
ioannisj be5ccfd
Merge branch 'feat/capture-exception' into feat/autocapture-exceptions
ioannisj fd00daf
feat: add async gap franes
ioannisj 42443b4
Revert "feat: add web support"
ioannisj edeaa36
Merge branch 'feat/capture-exception' into feat/autocapture-exceptions
ioannisj f9e9fb9
fix: simplify config
ioannisj db5f8a6
feat: add config for native autocapture
ioannisj a8bf280
fix: avoid null-assertion
ioannisj 3dca39d
fix: always restore original handlers
ioannisj cb2e261
fix: wrap error in PostHogException
ioannisj 488fedc
feat: add test cases
ioannisj bf5d806
feat: capture additional details from FlutterErrorDetails
ioannisj 4ec5010
fix: make install and uninstall methods private
ioannisj 7e331da
fix: do not modify behavior
ioannisj a9da171
feat: add current isolate error handling
ioannisj 2bea94e
fix: remove tests
ioannisj d0ee077
fix: annotate as internal
ioannisj c553ceb
fix: flutter analyze
ioannisj 61599ff
fix: do not install PlatformDispatcher.instance.onError on web
ioannisj fb4bc95
chore: update changelog
ioannisj 2c02178
Merge branch 'main' into feat/autocapture-exceptions
ioannisj 7c7fd1a
fix: build
ioannisj 042b1d0
chore: update changelog
ioannisj f06c1e2
fix: address feedback
ioannisj a9ec4ac
fix: mark IsolateErrorHandler as internal for now
ioannisj 3d06c85
fix: type inference
ioannisj 402a8ee
fix: update changelog
ioannisj 277ffb9
fix: platform check
ioannisj adf48db
chore: update sample project
ioannisj 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
Some comments aren't visible on the classic Files Changed page.
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
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
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,33 @@ | ||
| import 'dart:isolate'; | ||
|
|
||
| import 'package:posthog_flutter/src/util/logging.dart'; | ||
|
|
||
| /// Native platform implementation of isolate error handling | ||
| class IsolateErrorHandler { | ||
ioannisj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| RawReceivePort? _isolateErrorPort; | ||
|
|
||
| /// Add error listener to current isolate (should be main isolate) | ||
| void addErrorListener(Function(dynamic) onError) { | ||
| // In Flutter, the main isolate typically has debugName 'main' | ||
| final isolateName = Isolate.current.debugName; | ||
| if (isolateName != null && isolateName != 'main') { | ||
| printIfDebug( | ||
| 'PostHog isolate error handler is being set up in isolate "$isolateName" instead of main isolate'); | ||
| } | ||
ioannisj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| _isolateErrorPort = RawReceivePort(onError); | ||
| Isolate.current.addErrorListener(_isolateErrorPort!.sendPort); | ||
| } | ||
|
|
||
| /// Remove error listener and clean up | ||
| void removeErrorListener() { | ||
| if (_isolateErrorPort != null) { | ||
| _isolateErrorPort!.close(); | ||
ioannisj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Isolate.current.removeErrorListener(_isolateErrorPort!.sendPort); | ||
| _isolateErrorPort = null; | ||
| } | ||
| } | ||
|
|
||
| /// Check if error listener is active | ||
| bool get isActive => _isolateErrorPort != null; | ||
| } | ||
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,16 @@ | ||
| /// Web platform stub implementation of isolate error handling | ||
| /// Isolates are not available on web, so this is a no-op implementation | ||
| class IsolateErrorHandler { | ||
| /// Add error listener to current isolate (no-op on web) | ||
| void addErrorListener(Function(dynamic) onError) { | ||
| // No-op: Isolates are not available on web | ||
| } | ||
|
|
||
| /// Remove error listener and clean up (no-op on web) | ||
| void removeErrorListener() { | ||
| // No-op: Isolates are not available on web | ||
| } | ||
|
|
||
| /// Check if error listener is active (always false on web) | ||
| bool get isActive => false; | ||
| } |
234 changes: 234 additions & 0 deletions
234
lib/src/error_tracking/posthog_error_tracking_autocapture_integration.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,234 @@ | ||
| import 'dart:ui'; | ||
|
|
||
| import 'package:flutter/foundation.dart'; | ||
| import 'package:posthog_flutter/src/util/platform_io_stub.dart'; | ||
|
|
||
| import 'isolate_handler_io.dart' | ||
| if (dart.library.html) 'isolate_handler_web.dart'; | ||
| import 'package:posthog_flutter/src/util/logging.dart'; | ||
|
|
||
| import '../posthog_flutter_platform_interface.dart'; | ||
| import '../posthog_config.dart'; | ||
| import 'posthog_exception.dart'; | ||
|
|
||
| /// Handles automatic capture of Flutter and Dart exceptions | ||
| class PostHogErrorTrackingAutoCaptureIntegration { | ||
ioannisj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| final PostHogErrorTrackingConfig _config; | ||
| final PosthogFlutterPlatformInterface _posthog; | ||
|
|
||
| // Store original handlers (we'll chain with them from our handler) | ||
| FlutterExceptionHandler? _originalFlutterErrorHandler; | ||
| ErrorCallback? _originalPlatformErrorHandler; | ||
|
|
||
| // Isolate error handling | ||
| final IsolateErrorHandler _isolateErrorHandler = IsolateErrorHandler(); | ||
|
|
||
| bool _isEnabled = false; | ||
|
|
||
| static PostHogErrorTrackingAutoCaptureIntegration? _instance; | ||
|
|
||
| PostHogErrorTrackingAutoCaptureIntegration._({ | ||
| required PostHogErrorTrackingConfig config, | ||
| required PosthogFlutterPlatformInterface posthog, | ||
| }) : _config = config, | ||
| _posthog = posthog; | ||
|
|
||
| /// Install the autocapture integration (can only be installed once) | ||
| static PostHogErrorTrackingAutoCaptureIntegration? install({ | ||
| required PostHogErrorTrackingConfig config, | ||
| required PosthogFlutterPlatformInterface posthog, | ||
| }) { | ||
| if (_instance != null) { | ||
| debugPrint( | ||
| 'PostHog: Error tracking autocapture integration is already installed. Call PostHogErrorTrackingAutoCaptureIntegration.uninstall() first.'); | ||
| return null; | ||
| } | ||
|
|
||
| final instance = PostHogErrorTrackingAutoCaptureIntegration._( | ||
| config: config, | ||
| posthog: posthog, | ||
| ); | ||
|
|
||
| _instance = instance; | ||
|
|
||
| if (config.captureFlutterErrors || | ||
| config.capturePlatformDispatcherErrors || | ||
| config.captureIsolateErrors) { | ||
| instance.start(); | ||
| } | ||
|
|
||
| return instance; | ||
| } | ||
|
|
||
| /// Uninstall the autocapture integration | ||
| static void uninstall() { | ||
| if (_instance != null) { | ||
| _instance!.stop(); | ||
| _instance = null; | ||
| } | ||
| } | ||
|
|
||
| /// Start automatic exception capture | ||
| void start() { | ||
| if (_isEnabled) return; | ||
|
|
||
| _isEnabled = true; | ||
|
|
||
| // Set up Flutter error handler if enabled | ||
| if (_config.captureFlutterErrors) { | ||
| _setupFlutterErrorHandler(); | ||
| } | ||
|
|
||
| // Set up platform error handler if enabled | ||
| if (_config.capturePlatformDispatcherErrors) { | ||
| _setupPlatformErrorHandler(); | ||
| } | ||
|
|
||
| // Set up isolate error handler if enabled | ||
| if (_config.captureIsolateErrors) { | ||
| _setupIsolateErrorHandler(); | ||
| } | ||
| } | ||
|
|
||
| /// Stop automatic exception capture (restores original handlers) | ||
| void stop() { | ||
| if (!_isEnabled) return; | ||
|
|
||
| _isEnabled = false; | ||
|
|
||
| // Restore original handlers only if our own handler is still set | ||
| if (FlutterError.onError == _posthogFlutterErrorHandler) { | ||
| FlutterError.onError = _originalFlutterErrorHandler; | ||
| } | ||
| if (PlatformDispatcher.instance.onError == _posthogPlatformErrorHandler) { | ||
| PlatformDispatcher.instance.onError = _originalPlatformErrorHandler; | ||
| } | ||
|
|
||
| // Clean up isolate error handler | ||
| _isolateErrorHandler.removeErrorListener(); | ||
|
|
||
| // release refs | ||
| _originalFlutterErrorHandler = null; | ||
| _originalPlatformErrorHandler = null; | ||
| } | ||
|
|
||
| /// Flutter framework error handler | ||
| void _setupFlutterErrorHandler() { | ||
| // prevent circular calls | ||
| if (FlutterError.onError == _posthogFlutterErrorHandler) { | ||
| return; | ||
| } | ||
|
|
||
| _originalFlutterErrorHandler = FlutterError.onError; | ||
|
|
||
| FlutterError.onError = _posthogFlutterErrorHandler; | ||
| } | ||
|
|
||
| void _posthogFlutterErrorHandler(FlutterErrorDetails details) { | ||
| if (!details.silent || _config.captureSilentFlutterErrors) { | ||
| // Collect additional context information | ||
| //(see: https://github.com/getsentry/sentry-dart/blob/a69a51fd1695dd93024be80a50ad05dd990b2b82/packages/flutter/lib/src/integrations/flutter_error_integration.dart#L35-L60) | ||
| final context = details.context?.toDescription(); | ||
| final collector = details.informationCollector?.call() ?? []; | ||
| final information = collector.isNotEmpty | ||
| ? (StringBuffer()..writeAll(collector, '\n')).toString() | ||
| : null; | ||
| final library = details.library; | ||
| final errorSummary = details.toStringShort(); | ||
|
|
||
| // Build additional properties with Flutter-specific details | ||
| final flutterErrorDetails = <String, Object>{ | ||
| if (context != null) 'context': 'thrown $context', | ||
ioannisj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (information != null) 'information': information, | ||
| if (library != null) 'library': library, | ||
| 'error_summary': errorSummary, | ||
| 'silent': details.silent, | ||
| }; | ||
|
|
||
| final wrappedError = PostHogException( | ||
| source: details.exception, mechanism: 'FlutterError', handled: false); | ||
|
|
||
| _captureException( | ||
ioannisj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| error: wrappedError, | ||
| stackTrace: details.stack, | ||
| properties: {'flutter_error_details': flutterErrorDetails}, | ||
| ); | ||
| } else { | ||
| printIfDebug( | ||
| "Error not captured because FlutterErrorDetails.silent is true and captureSilentFlutterErrors is false"); | ||
| } | ||
|
|
||
| // Call the original handler, if any | ||
| _originalFlutterErrorHandler?.call(details); | ||
| } | ||
|
|
||
| /// Platform error handler for Dart runtime errors | ||
| void _setupPlatformErrorHandler() { | ||
| // On web, PlatformDispatcher.onError is not implemented. Skip for now | ||
| // See: https://github.com/flutter/flutter/issues/100277 | ||
| if (!isSupportedPlatform()) { | ||
| return; | ||
| } | ||
|
|
||
| // prevent circular calls | ||
| if (PlatformDispatcher.instance.onError == _posthogPlatformErrorHandler) { | ||
ioannisj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
|
|
||
| _originalPlatformErrorHandler = PlatformDispatcher.instance.onError; | ||
| PlatformDispatcher.instance.onError = _posthogPlatformErrorHandler; | ||
| } | ||
|
|
||
| bool _posthogPlatformErrorHandler(Object error, StackTrace stackTrace) { | ||
| final wrappedError = PostHogException( | ||
| source: error, | ||
| mechanism: 'PlatformDispatcher', | ||
| handled: false, | ||
| ); | ||
|
|
||
| _captureException(error: wrappedError, stackTrace: stackTrace); | ||
|
|
||
| // Call the original handler, if any | ||
| // False otherwise, so that default fallback mechanism is used | ||
| return _originalPlatformErrorHandler?.call(error, stackTrace) ?? false; | ||
| } | ||
|
|
||
| /// Isolate error handler for current isolate errors | ||
| void _setupIsolateErrorHandler() { | ||
| if (!_config.captureIsolateErrors) { | ||
| return; | ||
| } | ||
|
|
||
| _isolateErrorHandler.addErrorListener(_posthogIsolateErrorHandler); | ||
| } | ||
|
|
||
| void _posthogIsolateErrorHandler(dynamic error) { | ||
ioannisj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Isolate errors come as List<dynamic> with [errorString, stackTraceString] | ||
| // See: https://api.dartlang.org/stable/2.7.0/dart-isolate/Isolate/addErrorListener.html | ||
| if (error is List && error.length == 2) { | ||
| final String errorString = error.first; | ||
| final String? stackTraceString = error.last; | ||
ioannisj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| final wrappedError = PostHogException( | ||
| source: errorString, | ||
| mechanism: 'isolateError', | ||
| handled: false, | ||
| ); | ||
|
|
||
| final stackTrace = stackTraceString != null | ||
| ? StackTrace.fromString(stackTraceString) | ||
ioannisj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| : null; | ||
|
|
||
| _captureException(error: wrappedError, stackTrace: stackTrace); | ||
| } | ||
| } | ||
|
|
||
| Future<void> _captureException({ | ||
| required PostHogException error, | ||
| required StackTrace? stackTrace, | ||
| Map<String, Object>? properties, | ||
| }) { | ||
| return _posthog.captureException( | ||
| error: error, stackTrace: stackTrace, properties: properties); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.