Skip to content

Commit

Permalink
Set platform in sentry frames for better raw stacktrace representation (
Browse files Browse the repository at this point in the history
#2193)

* add platform to stacktrace

* update

* Update CHANGELOG

* Fix tests

* Fix test
  • Loading branch information
buenaflor authored Jul 24, 2024
1 parent 7f14ddd commit 7faee57
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Features

- Add dart platform to sentry frames ([#2193](https://github.com/getsentry/sentry-dart/pull/2193))
- This allows viewing the correct dart formatted raw stacktrace in the Sentry UI
- Support `ignoredExceptionsForType` ([#2150](https://github.com/getsentry/sentry-dart/pull/2150))
- Filter out exception types by calling `SentryOptions.addExceptionFilterForType(Type exceptionType)`

Expand Down
2 changes: 2 additions & 0 deletions dart/lib/src/sentry_stack_trace_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class SentryStackTraceFactory {
// least we get an indication something's wrong and are able to fix it.
}

final platform = _options.platformChecker.isWeb ? 'javascript' : 'dart';
final fileName =
frame.uri.pathSegments.isNotEmpty ? frame.uri.pathSegments.last : null;
final abs = '$eventOrigin${_absolutePathForCrashReport(frame)}';
Expand All @@ -114,6 +115,7 @@ class SentryStackTraceFactory {
inApp: _isInApp(frame),
fileName: fileName,
package: frame.package,
platform: platform,
);

final line = frame.line;
Expand Down
52 changes: 43 additions & 9 deletions dart/test/stack_trace_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:stack_trace/stack_trace.dart';
import 'package:test/test.dart';

import 'mocks.dart';
import 'mocks/mock_platform_checker.dart';

void main() {
group('encodeStackTraceFrame', () {
Expand All @@ -23,7 +24,8 @@ void main() {
'lineno': 1,
'colno': 2,
'in_app': false,
'filename': 'core'
'filename': 'core',
'platform': 'dart',
},
);
});
Expand Down Expand Up @@ -123,15 +125,17 @@ void main() {
'lineno': 46,
'colno': 9,
'in_app': true,
'filename': 'test.dart'
'filename': 'test.dart',
'platform': 'dart',
},
{
'abs_path': '${eventOrigin}test.dart',
'function': 'baz',
'lineno': 50,
'colno': 3,
'in_app': true,
'filename': 'test.dart'
'filename': 'test.dart',
'platform': 'dart',
},
]);
});
Expand All @@ -152,7 +156,8 @@ void main() {
'lineno': 46,
'colno': 9,
'in_app': true,
'filename': 'test.dart'
'filename': 'test.dart',
'platform': 'dart',
},
{
'abs_path': '<asynchronous suspension>',
Expand All @@ -163,7 +168,8 @@ void main() {
'lineno': 50,
'colno': 3,
'in_app': true,
'filename': 'test.dart'
'filename': 'test.dart',
'platform': 'dart',
},
]);
});
Expand Down Expand Up @@ -229,22 +235,25 @@ isolate_instructions: 10fa27070, vm_instructions: 10fa21e20
'function': 'PlatformDispatcher._dispatchPointerDataPacket',
'lineno': 341,
'abs_path': '${eventOrigin}dart:ui/platform_dispatcher.dart',
'in_app': false
'in_app': false,
'platform': 'dart',
},
{
'filename': 'main.dart',
'package': 'example',
'function': 'MainScaffold.build.<fn>',
'lineno': 131,
'abs_path': '${eventOrigin}package:example/main.dart',
'in_app': true
'in_app': true,
'platform': 'dart',
},
{
'filename': 'main.dart',
'function': 'asyncThrows',
'lineno': 404,
'abs_path': '${eventOrigin}main.dart',
'in_app': true
'in_app': true,
'platform': 'dart',
}
]);
});
Expand All @@ -258,6 +267,29 @@ isolate_instructions: 10fa27070, vm_instructions: 10fa21e20
.map((frame) => frame.toJson());
expect(frames.isEmpty, true);
});

test('sets platform to javascript for web and dart for non-web', () {
final frame = Frame(Uri.parse('file://foo/bar/baz.dart'), 1, 2, 'buzz');
final fixture = Fixture();

// Test for web platform
final webSut = fixture.getSut(isWeb: true);
var webFrame = webSut.encodeStackTraceFrame(frame)!;
expect(webFrame.platform, 'javascript');

// Test for non-web platform
final nativeFrameBeforeSut = fixture.getSut(isWeb: false);
var nativeFrameBefore =
nativeFrameBeforeSut.encodeStackTraceFrame(frame)!;
expect(nativeFrameBefore.platform, 'dart');

// Test when platform is already set
final frameWithPlatform = fixture
.getSut()
.encodeStackTraceFrame(frame)!
.copyWith(platform: 'native');
expect(frameWithPlatform.platform, 'native');
});
});
}

Expand All @@ -266,8 +298,10 @@ class Fixture {
List<String> inAppIncludes = const [],
List<String> inAppExcludes = const [],
bool considerInAppFramesByDefault = true,
bool isWeb = false,
}) {
final options = SentryOptions(dsn: fakeDsn);
final options = SentryOptions(
dsn: fakeDsn, checker: MockPlatformChecker(isWebValue: isWeb));
inAppIncludes.forEach(options.addInAppInclude);
inAppExcludes.forEach(options.addInAppExclude);
options.considerInAppFramesByDefault = considerInAppFramesByDefault;
Expand Down
10 changes: 9 additions & 1 deletion dart/test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,15 @@ Future testCaptureException(
}
expect(
topFrame.keys,
<String>['filename', 'function', 'lineno', 'colno', 'abs_path', 'in_app'],
<String>[
'filename',
'function',
'lineno',
'colno',
'abs_path',
'in_app',
'platform'
],
);

if (isWeb) {
Expand Down

0 comments on commit 7faee57

Please sign in to comment.