-
-
Notifications
You must be signed in to change notification settings - Fork 257
Feat/dart default integrations #187
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
Changes from 20 commits
95b2f92
df76b33
22bf3cf
df9df2c
34151dd
b165efb
71ce158
8423efa
d65c61e
2275f3a
abe6c67
3c07c9f
44706b2
3658395
b2ba775
37d6371
513898b
bfddf96
986d95d
d852ee6
262d91e
26c0f9d
e74acb9
dfc7a64
ad6130d
0f42f6a
57dfc45
c083456
96c2f95
ee21afa
a33841b
61a6f11
b4dc147
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,10 +40,48 @@ import 'dart:async'; | |
import 'package:sentry/sentry.dart'; | ||
|
||
Future<void> main() async { | ||
await Sentry.init((options) { | ||
options.dsn = 'https://[email protected]/add-your-dsn-here'; | ||
}); | ||
await Sentry.init( | ||
(options) { | ||
options.dsn = 'https://[email protected]/add-your-dsn-here'; | ||
}, | ||
initApp, // Init your App. | ||
); | ||
} | ||
|
||
void initApp() { | ||
marandaneto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try { | ||
aMethodThatMightFail(); | ||
} catch (exception, stackTrace) { | ||
await Sentry.captureException( | ||
exception, | ||
stackTrace: stackTrace, | ||
); | ||
} | ||
} | ||
|
||
void aMethodThatMightFail() { | ||
throw null; | ||
} | ||
``` | ||
|
||
Or, if you don't want to run your app in its own error zone [runZonedGuarded] : | ||
marandaneto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```dart | ||
import 'dart:async'; | ||
import 'package:sentry/sentry.dart'; | ||
|
||
Future<void> main() async { | ||
await Sentry.init( | ||
(options) { | ||
options.dsn = 'https://[email protected]/add-your-dsn-here'; | ||
}, | ||
); | ||
|
||
// Init your App. | ||
initApp(); | ||
} | ||
|
||
void initApp() { | ||
marandaneto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try { | ||
aMethodThatMightFail(); | ||
} catch (exception, stackTrace) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
// found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
import 'dart:io'; | ||
|
||
import 'package:sentry/sentry.dart'; | ||
|
||
|
@@ -17,9 +18,12 @@ Future<void> main() async { | |
SentryEvent processTagEvent(SentryEvent event, Object hint) => | ||
event..tags.addAll({'page-locale': 'en-us'}); | ||
|
||
await Sentry.init((options) => options | ||
..dsn = dsn | ||
..addEventProcessor(processTagEvent)); | ||
await Sentry.init( | ||
(options) => options | ||
..dsn = dsn | ||
..addEventProcessor(processTagEvent), | ||
runApp, | ||
); | ||
|
||
Sentry.addBreadcrumb( | ||
Breadcrumb( | ||
|
@@ -48,7 +52,9 @@ Future<void> main() async { | |
..setTag('build', '579') | ||
..setExtra('company-name', 'Dart Inc'); | ||
}); | ||
} | ||
|
||
void runApp() async { | ||
marandaneto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print('\nReporting a complete event example: '); | ||
|
||
// Sends a full Sentry event payload to show the different parts of the UI. | ||
|
@@ -82,6 +88,8 @@ Future<void> main() async { | |
} finally { | ||
await Sentry.close(); | ||
} | ||
|
||
exit(0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need this if we There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, without it the dart process seems to never end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dart does not end the process or it gets a deadlock somewhere in our SDK? worth checking There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it seems to be caused by the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅ I removed the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but the problem still exists or what? just to know if you found out or not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, the problem still exists. it's caused by the isolateErrorIntegration, but didn't found how to fix it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. task was not created, so I did myself |
||
} | ||
|
||
Future<void> loadConfig() async { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,24 +11,15 @@ const dsn = | |
'https://[email protected]/5428562'; | ||
|
||
Future<void> main() async { | ||
querySelector('#output').text = 'Your Dart app is running.'; | ||
|
||
querySelector('#btEvent') | ||
.onClick | ||
.listen((event) => captureCompleteExampleEvent()); | ||
querySelector('#btMessage').onClick.listen((event) => captureMessage()); | ||
querySelector('#btException').onClick.listen((event) => captureException()); | ||
|
||
await initSentry(); | ||
} | ||
|
||
Future<void> initSentry() async { | ||
SentryEvent processTagEvent(SentryEvent event, Object hint) => | ||
event..tags.addAll({'page-locale': 'en-us'}); | ||
|
||
await Sentry.init((options) => options | ||
..dsn = dsn | ||
..addEventProcessor(processTagEvent)); | ||
await Sentry.init( | ||
(options) => options | ||
..dsn = dsn | ||
..addEventProcessor(processTagEvent), | ||
runApp, | ||
); | ||
|
||
Sentry.addBreadcrumb( | ||
Breadcrumb( | ||
|
@@ -59,6 +50,18 @@ Future<void> initSentry() async { | |
}); | ||
} | ||
|
||
void runApp() { | ||
print('runApp'); | ||
|
||
querySelector('#output').text = 'Your Dart app is running.'; | ||
|
||
querySelector('#btEvent') | ||
.onClick | ||
.listen((event) => captureCompleteExampleEvent()); | ||
querySelector('#btMessage').onClick.listen((event) => captureMessage()); | ||
querySelector('#btException').onClick.listen((event) => captureException()); | ||
} | ||
|
||
void captureMessage() async { | ||
print('Capturing Message : '); | ||
final sentryId = await Sentry.captureMessage( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import 'dart:async'; | ||
marandaneto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import 'hub.dart'; | ||
import 'protocol.dart'; | ||
import 'sentry.dart'; | ||
import 'sentry_options.dart'; | ||
import 'throwable_mechanism.dart'; | ||
|
||
/// integration that capture errors on the runZonedGuarded error handler | ||
Integration runZonedGuardedIntegration( | ||
AppRunner appRunner, | ||
) { | ||
void integration(Hub hub, SentryOptions options) { | ||
runZonedGuarded(() async { | ||
await appRunner(); | ||
}, (exception, stackTrace) async { | ||
// runZonedGuarded doesn't crash the App. | ||
const mechanism = Mechanism(type: 'runZonedGuarded', handled: true); | ||
final throwableMechanism = ThrowableMechanism(mechanism, exception); | ||
|
||
final event = SentryEvent( | ||
throwable: throwableMechanism, | ||
level: SentryLevel.fatal, | ||
); | ||
|
||
await hub.captureEvent(event, stackTrace: stackTrace); | ||
}); | ||
|
||
options.sdk.addIntegration('runZonedGuardedIntegration'); | ||
} | ||
|
||
return integration; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import 'dart:isolate'; | ||
|
||
import 'hub.dart'; | ||
import 'protocol.dart'; | ||
import 'sentry_options.dart'; | ||
import 'throwable_mechanism.dart'; | ||
|
||
/// integration that capture errors on the current Isolate Error handler | ||
/// which is the main thread. | ||
void isolateErrorIntegration(Hub hub, SentryOptions options) { | ||
final receivePort = _createPort(hub, options); | ||
|
||
Isolate.current.addErrorListener(receivePort.sendPort); | ||
|
||
options.sdk.addIntegration('isolateErrorIntegration'); | ||
} | ||
|
||
RawReceivePort _createPort(Hub hub, SentryOptions options) { | ||
return RawReceivePort( | ||
(dynamic error) async { | ||
await handleIsolateError(hub, options, error); | ||
}, | ||
); | ||
} | ||
|
||
/// Parse and raise an event out of the Isolate error. | ||
/// Visible for testing. | ||
Future<void> handleIsolateError( | ||
Hub hub, | ||
SentryOptions options, | ||
dynamic error, | ||
) async { | ||
options.logger(SentryLevel.debug, 'Capture from IsolateError $error'); | ||
|
||
// https://api.dartlang.org/stable/2.7.0/dart-isolate/Isolate/addErrorListener.html | ||
// error is a list of 2 elements | ||
if (error is List<dynamic> && error.length == 2) { | ||
final dynamic throwable = error.first; | ||
final dynamic stackTrace = error.last; | ||
|
||
// Isolate errors don't crash the App. | ||
const mechanism = Mechanism(type: 'isolateError', handled: true); | ||
final throwableMechanism = ThrowableMechanism(mechanism, throwable); | ||
final event = SentryEvent( | ||
throwable: throwableMechanism, | ||
level: SentryLevel.fatal, | ||
); | ||
|
||
await hub.captureEvent(event, stackTrace: stackTrace); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import 'hub.dart'; | ||
import 'sentry_options.dart'; | ||
|
||
// noop web integration : isolate doesnt' work in browser | ||
void isolateErrorIntegration(Hub hub, SentryOptions options) {} |
Uh oh!
There was an error while loading. Please reload this page.