Skip to content

Commit

Permalink
fix(app-start): accessing null json fields from fetchNativeAppStart (
Browse files Browse the repository at this point in the history
…#2340)

* update
  • Loading branch information
buenaflor authored Oct 10, 2024
1 parent 824c070 commit 547db82
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- Rounding error used on frames.total and reject frame measurements if frames.total is less than frames.slow or frames.frozen ([#2308](https://github.com/getsentry/sentry-dart/pull/2308))
- iOS replay integration when only `onErrorSampleRate` is specified ([#2306](https://github.com/getsentry/sentry-dart/pull/2306))
- Fix TTID timing issue ([#2326](https://github.com/getsentry/sentry-dart/pull/2326))
- Accessing invalid json fields from `fetchNativeAppStart` should return null ([#2340](https://github.com/getsentry/sentry-dart/pull/2340))
- Error when calling `SentryFlutter.reportFullyDisplayed()` twice ([#2339](https://github.com/getsentry/sentry-dart/pull/2339))

### Deprecate
Expand Down
28 changes: 23 additions & 5 deletions flutter/lib/src/native/native_app_start.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:meta/meta.dart';
import 'package:sentry/sentry.dart';

@internal
class NativeAppStart {
Expand All @@ -13,12 +14,29 @@ class NativeAppStart {
bool isColdStart;
Map<dynamic, dynamic> nativeSpanTimes;

factory NativeAppStart.fromJson(Map<String, dynamic> json) {
static NativeAppStart? fromJson(Map<String, dynamic> json) {
final appStartTime = json['appStartTime'];
final pluginRegistrationTime = json['pluginRegistrationTime'];
final isColdStart = json['isColdStart'];
final nativeSpanTimes = json['nativeSpanTimes'];

if (appStartTime is! double ||
pluginRegistrationTime is! int ||
isColdStart is! bool ||
nativeSpanTimes is! Map) {
// ignore: invalid_use_of_internal_member
Sentry.currentHub.options.logger(
SentryLevel.warning,
'Failed to parse json when capturing App Start metrics. App Start wont be reported.',
);
return null;
}

return NativeAppStart(
appStartTime: json['appStartTime'] as double,
pluginRegistrationTime: json['pluginRegistrationTime'] as int,
isColdStart: json['isColdStart'] as bool,
nativeSpanTimes: json['nativeSpanTimes'] as Map<dynamic, dynamic>,
appStartTime: appStartTime,
pluginRegistrationTime: pluginRegistrationTime,
isColdStart: isColdStart,
nativeSpanTimes: nativeSpanTimes,
);
}
}
15 changes: 15 additions & 0 deletions flutter/test/sentry_native_channel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ void main() {
expect(actual?.isColdStart, true);
});

test('invalid fetchNativeAppStart returns null', () async {
when(channel.invokeMethod('fetchNativeAppStart'))
.thenAnswer((_) async => {
'pluginRegistrationTime': 'invalid',
'appStartTime': 'invalid',
'isColdStart': 'invalid',
// ignore: inference_failure_on_collection_literal
'nativeSpanTimes': 'invalid',
});

final actual = await sut.fetchNativeAppStart();

expect(actual, isNull);
});

test('beginNativeFrames', () async {
when(channel.invokeMethod('beginNativeFrames'))
.thenAnswer((realInvocation) async {});
Expand Down

0 comments on commit 547db82

Please sign in to comment.