Skip to content

Commit

Permalink
Add hint in flutter error integration
Browse files Browse the repository at this point in the history
  • Loading branch information
denrase committed Oct 17, 2024
1 parent 0054c9d commit 6a2fb35
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
14 changes: 8 additions & 6 deletions flutter/lib/src/integrations/flutter_error_integration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ class FlutterErrorIntegration implements Integration<SentryFlutterOptions> {

final hint = Hint();
hint.addAll({TypeCheckHint.syntheticException: errorDetails});

await hub.captureEvent(event,
// ignore: invalid_use_of_internal_member
stackTrace: errorDetails.stack ?? getCurrentStackTrace(),
hint:
Hint.withMap({TypeCheckHint.syntheticException: errorDetails}));
var stackTrace = errorDetails.stack;
if (errorDetails.stack == null ||
errorDetails.stack == StackTrace.empty) {
// ignore: invalid_use_of_internal_member
stackTrace = getCurrentStackTrace();
hint.addAll({TypeCheckHint.currentStackTrace: true});
}
await hub.captureEvent(event, stackTrace: stackTrace, hint: hint);
// we don't call Zone.current.handleUncaughtError because we'd like
// to set a specific mechanism for FlutterError.onError.
} else {
Expand Down
50 changes: 50 additions & 0 deletions flutter/test/integrations/flutter_error_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void main() {
FlutterExceptionHandler? handler,
dynamic exception,
FlutterErrorDetails? optionalDetails,
StackTrace? stackTrace,
}) {
_mockValues();

Expand All @@ -53,6 +54,7 @@ void main() {
context: DiagnosticsNode.message('while handling a gesture'),
library: 'sentry',
informationCollector: () => [DiagnosticsNode.message('foo bar')],
stack: stackTrace,
);

FlutterError.reportError(optionalDetails ?? details);
Expand Down Expand Up @@ -310,6 +312,54 @@ void main() {

expect(event.level, SentryLevel.error);
});

test('adds current stack trace hint on null stack trace', () async {
final exception = StateError('error');

_reportError(exception: exception, stackTrace: null);

final hint = verify(
await fixture.hub.captureEvent(
captureAny,
stackTrace: captureAnyNamed('stackTrace'),
hint: captureAnyNamed('hint'),
),
).captured[2] as Hint;

expect(hint.get(TypeCheckHint.currentStackTrace), isTrue);
});

test('adds current stack trace hint on empty stack trace', () async {
final exception = StateError('error');

_reportError(exception: exception, stackTrace: StackTrace.empty);

final hint = verify(
await fixture.hub.captureEvent(
captureAny,
stackTrace: captureAnyNamed('stackTrace'),
hint: captureAnyNamed('hint'),
),
).captured[2] as Hint;

expect(hint.get(TypeCheckHint.currentStackTrace), isTrue);
});

test('does not add current stack trace hint with stack trace', () async {
final exception = StateError('error');

_reportError(exception: exception, stackTrace: StackTrace.current);

final hint = verify(
await fixture.hub.captureEvent(
captureAny,
stackTrace: captureAnyNamed('stackTrace'),
hint: captureAnyNamed('hint'),
),
).captured[2] as Hint;

expect(hint.get(TypeCheckHint.currentStackTrace), isNull);
});
});
}

Expand Down

0 comments on commit 6a2fb35

Please sign in to comment.