From 0480d39e627b06b3c03195a8a3845464e7a3c35f Mon Sep 17 00:00:00 2001 From: Diego Veralli Date: Thu, 19 Aug 2021 18:06:36 +0200 Subject: [PATCH] Fix missing code_version in payload and don't set optional fields if they're null --- .github/workflows/ci.yml | 2 +- rollbar_dart/lib/src/api/payload/data.dart | 16 +++++++++---- rollbar_dart/lib/src/core_notifier.dart | 4 ++-- rollbar_dart/test/rollbar_test.dart | 28 +++++++++++++++++++++- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8cda30b..b59c825 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ jobs: name: Flutter version ${{ matrix.flutter_version }} strategy: matrix: - flutter_version: ['2.0.5', '1.20.4'] + flutter_version: ['2.0.5', '1.20.4', '2.2.3'] steps: - uses: actions/checkout@v2 diff --git a/rollbar_dart/lib/src/api/payload/data.dart b/rollbar_dart/lib/src/api/payload/data.dart index c788c94..1144679 100644 --- a/rollbar_dart/lib/src/api/payload/data.dart +++ b/rollbar_dart/lib/src/api/payload/data.dart @@ -10,6 +10,7 @@ class Data { String platform; String language; String framework; + String codeVersion; Level level; int timestamp; Body body; @@ -24,17 +25,22 @@ class Data { 'client': client.toJson(), 'platform': platform, 'language': language, - 'framework': framework, 'level': level.name, 'timestamp': timestamp, 'body': body.toJson(), - 'custom': custom, }; - if (platformPayload != null) { - result['platform_payload'] = platformPayload; - } + addIfNotNull(result, 'framework', framework); + addIfNotNull(result, 'code_version', codeVersion); + addIfNotNull(result, 'custom', custom); + addIfNotNull(result, 'platform_payload', platformPayload); return result; } + + void addIfNotNull(Map result, String name, dynamic value) { + if (value != null) { + result[name] = value; + } + } } diff --git a/rollbar_dart/lib/src/core_notifier.dart b/rollbar_dart/lib/src/core_notifier.dart index 9475c8c..d62e065 100644 --- a/rollbar_dart/lib/src/core_notifier.dart +++ b/rollbar_dart/lib/src/core_notifier.dart @@ -40,6 +40,8 @@ class CoreNotifier { ..language = 'dart' ..level = level ..platform = Platform.operatingSystem + ..framework = _config.framework + ..codeVersion = _config.codeVersion ..client = (Client() ..locale = Platform.localeName ..hostname = Platform.localHostname @@ -51,8 +53,6 @@ class CoreNotifier { ..environment = _config.environment ..notifier = {'version': NOTIFIER_VERSION, 'name': NOTIFIER_NAME}; - data.framework = _config.framework; - if (_transformer != null) { data = await _transformer.transform(error, stackTrace, data); } diff --git a/rollbar_dart/test/rollbar_test.dart b/rollbar_dart/test/rollbar_test.dart index 24f7175..5f46e55 100644 --- a/rollbar_dart/test/rollbar_test.dart +++ b/rollbar_dart/test/rollbar_test.dart @@ -20,7 +20,7 @@ void main() { // mock as the sender factory. var config = (ConfigBuilder('BlaBlaAccessToken') ..environment = 'production' - ..codeVersion = '1.0.0' + ..codeVersion = '0.23.2' ..handleUncaughtErrors = false ..sender = (_) => sender) .build(); @@ -36,6 +36,7 @@ void main() { await rollbar.error(error, stackTrace); var payload = verify(await sender.send(captureAny)).captured.single; + expect(payload['data']['code_version'], equals('0.23.2')); expect(payload['data']['level'], equals('error')); var trace = getPath(payload, ['data', 'body', 'trace']); @@ -46,6 +47,31 @@ void main() { } }); + test('If optional fields are not set they should not be added to the payload', + () async { + var config = (ConfigBuilder('BlaBlaAccessToken') + ..environment = 'production' + ..handleUncaughtErrors = false + ..sender = (_) => sender) + .build(); + + rollbar = Rollbar(config); + await rollbar.ensureInitialized(); + + try { + failingFunction(); + } catch (error, stackTrace) { + await rollbar.error(error, stackTrace); + var payload = verify(await sender.send(captureAny)).captured.single; + + Map data = payload['data']; + expect(data, isNot(contains('code_version'))); + expect(data, isNot(contains('framework'))); + expect(data, isNot(contains('custom'))); + expect(data, isNot(contains('platform_payload'))); + } + }); + test('If error trasformer is provided it should transform error', () async { var config = (ConfigBuilder('token') ..environment = 'production'