From f75f7c3183f667f0725af76d5dc944aaed6eafc6 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 22 Oct 2020 15:21:12 +0200 Subject: [PATCH 1/2] changelog --- CHANGELOG.md | 1 + dart/lib/src/client.dart | 2 +- dart/lib/src/sentry.dart | 23 +++++++++++++++++++---- dart/test/sentry_test.dart | 35 ++++++++++++++++++++++++++++++++--- 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bbcd28ab28..4063a2f2f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ - Ref: SentryId generates UUID #119 - Ref: Event now is SentryEvent and added GPU #121 - Feat: before breadcrumb and scope ref. #122 +- Ref: Sentry init with null and empty DSN and close method # `package:sentry` changelog diff --git a/dart/lib/src/client.dart b/dart/lib/src/client.dart index 12ff28b3b1..447a7e91da 100644 --- a/dart/lib/src/client.dart +++ b/dart/lib/src/client.dart @@ -184,7 +184,7 @@ abstract class SentryClient { return captureEvent(event, scope: scope); } - Future close() async { + void close() { options.httpClient?.close(); } diff --git a/dart/lib/src/sentry.dart b/dart/lib/src/sentry.dart index 3778b56b3d..2af2b17eee 100644 --- a/dart/lib/src/sentry.dart +++ b/dart/lib/src/sentry.dart @@ -37,7 +37,10 @@ class Sentry { ); } - _setDefaultConfiguration(options); + // if there's an empty DSN, SDK is disabled + if (!_setDefaultConfiguration(options)) { + return; + } final hub = currentHub; _hub = Hub(options); @@ -72,17 +75,29 @@ class Sentry { } /// Close the client SDK - static Future close() async => currentHub.close(); + static void close() { + final hub = currentHub; + _hub = NoOpHub(); + return hub.close(); + } /// Check if the current Hub is enabled/active. static bool get isEnabled => currentHub.isEnabled; - static void _setDefaultConfiguration(SentryOptions options) { - // TODO: check DSN nullability and empty + static bool _setDefaultConfiguration(SentryOptions options) { + if (options.dsn == null) { + throw ArgumentError.notNull( + 'DSN is required. Use empty string to disable SDK.'); + } + if (options.dsn.isEmpty) { + close(); + return false; + } if (options.debug && options.logger == noOpLogger) { options.logger = dartLogger; } + return true; } /// client injector only use for testing diff --git a/dart/test/sentry_test.dart b/dart/test/sentry_test.dart index 6f4067a1eb..f78ad86e41 100644 --- a/dart/test/sentry_test.dart +++ b/dart/test/sentry_test.dart @@ -5,7 +5,7 @@ import 'package:test/test.dart'; import 'mocks.dart'; void main() { - group('Sentry static entry', () { + group('Sentry capture methods', () { SentryClient client; Exception anException; @@ -17,9 +17,12 @@ void main() { client = MockSentryClient(); Sentry.initClient(client); }); + tearDown(() { + Sentry.close(); + }); - test('should capture the event', () { - Sentry.captureEvent(fakeEvent); + test('should capture the event', () async { + await Sentry.captureEvent(fakeEvent); verify( client.captureEvent( fakeEvent, @@ -55,4 +58,30 @@ void main() { ).called(1); }); }); + group('Sentry is enabled or closed', () { + test('null DSN', () { + expect( + () => Sentry.init((options) => options.dsn = null), + throwsArgumentError, + ); + expect(Sentry.isEnabled, false); + }); + + test('empty DSN', () { + Sentry.init((options) => options.dsn = ''); + expect(Sentry.isEnabled, false); + }); + + test('empty DSN', () { + Sentry.init((options) => options.dsn = fakeDsn); + + Sentry.initClient(MockSentryClient()); + + expect(Sentry.isEnabled, true); + + Sentry.close(); + + expect(Sentry.isEnabled, false); + }); + }); } From 793335ad23e7dac4691f13b830810d08ffd13f0a Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 22 Oct 2020 16:41:45 +0200 Subject: [PATCH 2/2] fix changelog --- .gitattributes | 1 + CHANGELOG.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000..a19ade077d --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +CHANGELOG.md merge=union diff --git a/CHANGELOG.md b/CHANGELOG.md index 236f1726cb..74df90e45e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,7 +27,7 @@ - Ref: SentryId generates UUID #119 - Ref: Event now is SentryEvent and added GPU #121 - Feat: before breadcrumb and scope ref. #122 -- Ref: Sentry init with null and empty DSN and close method +- Ref: Sentry init with null and empty DSN and close method #126 - Ref: Hint is passed across Sentry static class, Hub and Client #124 - Ref: Remove stackFrameFilter in favor of beforeSendCallback #125