From 108ac6caaf03d7121a55a78e57b872d2c2b5cd3c Mon Sep 17 00:00:00 2001 From: Erick Ghaumez Date: Wed, 28 Oct 2020 13:45:47 +0100 Subject: [PATCH] fix : throws on invalid dsn (#148) --- CHANGELOG.md | 1 + dart/example/main.dart | 2 -- dart/lib/src/protocol/dsn.dart | 14 +++++-------- dart/lib/src/sentry.dart | 3 +++ dart/lib/src/sentry_browser_client.dart | 11 +++------- dart/lib/src/sentry_io_client.dart | 8 +------- dart/lib/src/sentry_options.dart | 24 +++++++++++----------- dart/lib/src/transport/http_transport.dart | 2 +- 8 files changed, 26 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d31d7b27f..a3399e38b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ - Ref: rename the `throwable` argument name to `exception` in `captureEvents(...)` - Ref : remove dsn from Transport public Api - update the Dart sdk constraint +- fix : throws on invalid dsn # `package:sentry` changelog diff --git a/dart/example/main.dart b/dart/example/main.dart index 9559110536..2c7277e86e 100644 --- a/dart/example/main.dart +++ b/dart/example/main.dart @@ -81,8 +81,6 @@ Future main() async { } finally { await Sentry.close(); } - - /* TODO(rxlabz) Sentry CaptureMessage(message, level) */ } Future loadConfig() async { diff --git a/dart/lib/src/protocol/dsn.dart b/dart/lib/src/protocol/dsn.dart index 23c5b3edf2..04267f420a 100644 --- a/dart/lib/src/protocol/dsn.dart +++ b/dart/lib/src/protocol/dsn.dart @@ -46,15 +46,11 @@ class Dsn { final uri = Uri.parse(dsn); final userInfo = uri.userInfo.split(':'); - assert(() { - if (uri.pathSegments.isEmpty) { - throw ArgumentError( - 'Project ID not found in the URI path of the DSN URI: $dsn', - ); - } - - return true; - }()); + if (uri.pathSegments.isEmpty) { + throw ArgumentError( + 'Project ID not found in the URI path of the DSN URI: $dsn', + ); + } return Dsn( publicKey: userInfo[0], diff --git a/dart/lib/src/sentry.dart b/dart/lib/src/sentry.dart index 0ee4365472..8478a1c4ac 100644 --- a/dart/lib/src/sentry.dart +++ b/dart/lib/src/sentry.dart @@ -120,6 +120,9 @@ class Sentry { return false; } + // try parsing the dsn + Dsn.parse(options.dsn); + // if logger os NoOp, let's set a logger that prints on the console if (options.debug && options.logger == noOpLogger) { options.logger = dartLogger; diff --git a/dart/lib/src/sentry_browser_client.dart b/dart/lib/src/sentry_browser_client.dart index 2b43ed7957..68b841e07a 100644 --- a/dart/lib/src/sentry_browser_client.dart +++ b/dart/lib/src/sentry_browser_client.dart @@ -5,10 +5,8 @@ /// A pure Dart client for Sentry.io crash reporting. import 'dart:html' show window; -import 'protocol.dart'; import 'sentry_client.dart'; import 'sentry_options.dart'; -import 'version.dart'; SentryClient createSentryClient(SentryOptions options) => SentryBrowserClient(options); @@ -25,13 +23,10 @@ class SentryBrowserClient extends SentryClient { /// /// If [httpClient] is provided, it is used instead of the default client to /// make HTTP calls to Sentry.io. This is useful in tests. - factory SentryBrowserClient(SentryOptions options) { - options.sdk ??= Sdk(name: sdkName, version: sdkVersion); - - // origin is necessary for sentry to resolve stacktrace - return SentryBrowserClient._(options); - } + factory SentryBrowserClient(SentryOptions options) => + SentryBrowserClient._(options); SentryBrowserClient._(SentryOptions options) + // origin is necessary for sentry to resolve stacktrace : super.base(options, origin: '${window.location.origin}/'); } diff --git a/dart/lib/src/sentry_io_client.dart b/dart/lib/src/sentry_io_client.dart index ae7975af81..50db00c167 100644 --- a/dart/lib/src/sentry_io_client.dart +++ b/dart/lib/src/sentry_io_client.dart @@ -2,12 +2,9 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'protocol.dart'; - /// A pure Dart client for Sentry.io crash reporting. import 'sentry_client.dart'; import 'sentry_options.dart'; -import 'version.dart'; SentryClient createSentryClient(SentryOptions options) => SentryIOClient(options); @@ -15,10 +12,7 @@ SentryClient createSentryClient(SentryOptions options) => /// Logs crash reports and events to the Sentry.io service. class SentryIOClient extends SentryClient { /// Instantiates a client using [SentryOptions] - factory SentryIOClient(SentryOptions options) { - options.sdk ??= Sdk(name: sdkName, version: sdkVersion); - return SentryIOClient._(options); - } + factory SentryIOClient(SentryOptions options) => SentryIOClient._(options); SentryIOClient._(SentryOptions options) : super.base(options); } diff --git a/dart/lib/src/sentry_options.dart b/dart/lib/src/sentry_options.dart index 2e5a594cfa..d5e71c5e67 100644 --- a/dart/lib/src/sentry_options.dart +++ b/dart/lib/src/sentry_options.dart @@ -37,10 +37,10 @@ class SentryOptions { set httpClient(Client httpClient) => _httpClient = httpClient ?? _httpClient; - /// If [clock] is provided, it is used to get time instead of the system - /// clock. This is useful in tests. Should be an implementation of [ClockProvider]. ClockProvider _clock = getUtcDateTime; + /// If [clock] is provided, it is used to get time instead of the system + /// clock. This is useful in tests. Should be an implementation of [ClockProvider]. ClockProvider get clock => _clock; set clock(ClockProvider clock) => _clock = clock ?? _clock; @@ -48,42 +48,42 @@ class SentryOptions { /// This variable controls the total amount of breadcrumbs that should be captured Default is 100 int maxBreadcrumbs = 100; - /// Logger interface to log useful debugging information if debug is enabled Logger _logger = noOpLogger; + /// Logger interface to log useful debugging information if debug is enabled Logger get logger => _logger; set logger(Logger logger) { _logger = logger != null ? DiagnosticLogger(logger, this) : noOpLogger; } - /// Are callbacks that run for every event. They can either return a new event which in most cases - /// means just adding data OR return null in case the event will be dropped and not sent. final List _eventProcessors = []; + /// Are callbacks that run for every event. They can either return a new event which in most cases + /// means just adding data OR return null in case the event will be dropped and not sent. List get eventProcessors => List.unmodifiable(_eventProcessors); - /// Code that provides middlewares, bindings or hooks into certain frameworks or environments, - /// along with code that inserts those bindings and activates them. final List _integrations = []; // TODO: shutdownTimeout, flushTimeoutMillis // https://api.dart.dev/stable/2.10.2/dart-io/HttpClient/close.html doesn't have a timeout param, we'd need to implement manually + /// Code that provides middlewares, bindings or hooks into certain frameworks or environments, + /// along with code that inserts those bindings and activates them. List get integrations => List.unmodifiable(_integrations); /// Turns debug mode on or off. If debug is enabled SDK will attempt to print out useful debugging /// information if something goes wrong. Default is disabled. bool debug = false; - /// minimum LogLevel to be used if debug is enabled SentryLevel _diagnosticLevel = defaultDiagnosticLevel; set diagnosticLevel(SentryLevel level) { _diagnosticLevel = level ?? defaultDiagnosticLevel; } + /// minimum LogLevel to be used if debug is enabled SentryLevel get diagnosticLevel => _diagnosticLevel; /// Sentry client name used for the HTTP authHeader and userAgent eg @@ -111,17 +111,17 @@ class SentryOptions { /// sent. Events are picked randomly. Default is null (disabled) double sampleRate; + final List _inAppExcludes = []; + /// A list of string prefixes of module names that do not belong to the app, but rather third-party /// packages. Modules considered not to be part of the app will be hidden from stack traces by /// default. - final List _inAppExcludes = []; - List get inAppExcludes => List.unmodifiable(_inAppExcludes); - /// A list of string prefixes of module names that belong to the app. This option takes precedence - /// over inAppExcludes. final List _inAppIncludes = []; + /// A list of string prefixes of module names that belong to the app. This option takes precedence + /// over inAppExcludes. List get inAppIncludes => List.unmodifiable(_inAppIncludes); Transport _transport = NoOpTransport(); diff --git a/dart/lib/src/transport/http_transport.dart b/dart/lib/src/transport/http_transport.dart index 0330d5ecc3..b56f443155 100644 --- a/dart/lib/src/transport/http_transport.dart +++ b/dart/lib/src/transport/http_transport.dart @@ -28,7 +28,7 @@ class HttpTransport implements Transport { _dsn = Dsn.parse(options.dsn), _headers = _buildHeaders(sdkIdentifier: options.sdk.identifier) { _credentialBuilder = CredentialBuilder( - dsn: Dsn.parse(options.dsn), + dsn: _dsn, clientId: options.sdk.identifier, clock: options.clock, );