Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: execute integrations on hub creation
  • Loading branch information
marandaneto committed Oct 26, 2020
commit ae6ea2c02391b3ab8ba2ec64bb25bc2425c51e6b
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- Ref: execute before send callback
- Feat: add lastEventId to the Sentry static API
- Feat: addBreadcrumb on Static API
- Fix: Integrations are executed on Hub creation

# `package:sentry` changelog

Expand Down
7 changes: 7 additions & 0 deletions dart/lib/src/hub.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:async';
import 'dart:collection';

import 'package:sentry/src/hub_adapter.dart';

import 'client.dart';
import 'noop_client.dart';
import 'protocol.dart';
Expand Down Expand Up @@ -31,6 +33,11 @@ class Hub {
Hub._(SentryOptions options) : _options = options {
_stack.add(_StackItem(_getClient(_options), Scope(_options)));
_isEnabled = true;

// execute integrations
options.integrations.forEach((integration) {
integration(HubAdapter(), _options);
});
}

static void _validateOptions(SentryOptions options) {
Expand Down
73 changes: 73 additions & 0 deletions dart/lib/src/hub_adapter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import 'package:sentry/src/protocol/sentry_level.dart';
import 'package:sentry/src/protocol/sentry_id.dart';
import 'package:sentry/src/protocol/sentry_event.dart';
import 'package:sentry/src/protocol/breadcrumb.dart';
import 'package:sentry/src/client.dart';
import 'package:sentry/src/sentry.dart';
import 'hub.dart';

/// Hub adapter to make Integrations testable
class HubAdapter implements Hub {
HubAdapter._();

static final HubAdapter _instance = HubAdapter._();

factory HubAdapter() {
return _instance;
}

@override
void addBreadcrumb(Breadcrumb crumb, {dynamic hint}) =>
Sentry.addBreadcrumb(crumb, hint: hint);

@override
void bindClient(SentryClient client) => Sentry.bindClient(client);

@override
Future<SentryId> captureEvent(SentryEvent event, {dynamic hint}) =>
Sentry.captureEvent(event, hint: hint);

@override
Future<SentryId> captureException(
dynamic throwable, {
dynamic stackTrace,
dynamic hint,
}) =>
Sentry.captureException(
throwable,
stackTrace: stackTrace,
hint: hint,
);

@override
Future<SentryId> captureMessage(
String message, {
SentryLevel level = SentryLevel.info,
String template,
List params,
dynamic hint,
}) =>
Sentry.captureMessage(
message,
level: level,
template: template,
params: params,
hint: hint,
);

@override
Hub clone() => Sentry.clone();

@override
void close() => Sentry.close();

@override
void configureScope(ScopeCallback callback) =>
Sentry.configureScope(callback);

@override
bool get isEnabled => Sentry.isEnabled;

@override
SentryId get lastEventId => Sentry.lastEventId;
}
57 changes: 33 additions & 24 deletions dart/lib/src/sentry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,35 @@ class Sentry {
static Future<SentryId> captureEvent(
SentryEvent event, {
dynamic hint,
}) async {
return currentHub.captureEvent(event, hint: hint);
}
}) async =>
currentHub.captureEvent(event, hint: hint);

/// Reports the [exception] and optionally its [stackTrace] to Sentry.io.
static Future<SentryId> captureException(
dynamic error, {
dynamic throwable, {
dynamic stackTrace,
dynamic hint,
}) async {
return currentHub.captureException(
error,
stackTrace: stackTrace,
hint: hint,
);
}
}) async =>
currentHub.captureException(
throwable,
stackTrace: stackTrace,
hint: hint,
);

Future<SentryId> captureMessage(
static Future<SentryId> captureMessage(
String message, {
SentryLevel level,
String template,
List<dynamic> params,
dynamic hint,
}) async {
return currentHub.captureMessage(
message,
level: level,
template: template,
params: params,
hint: hint,
);
}
}) async =>
currentHub.captureMessage(
message,
level: level,
template: template,
params: params,
hint: hint,
);

/// Close the client SDK
static void close() {
Expand All @@ -98,20 +95,32 @@ class Sentry {
static SentryId get lastEventId => currentHub.lastEventId;

/// Adds a breacrumb to the current Scope
static void addBreadcrumb(Breadcrumb crumb, {dynamic hint}) {
currentHub.addBreadcrumb(crumb, hint: hint);
}
static void addBreadcrumb(Breadcrumb crumb, {dynamic hint}) =>
currentHub.addBreadcrumb(crumb, hint: hint);

/// Configures the scope through the callback.
static void configureScope(ScopeCallback callback) =>
currentHub.configureScope(callback);

/// Clones the current Hub
static Hub clone() => currentHub.clone();

/// Binds a different client to the current hub
static void bindClient(SentryClient client) => currentHub.bindClient(client);

static bool _setDefaultConfiguration(SentryOptions options) {
// if DSN is null, let's crash the App.
if (options.dsn == null) {
throw ArgumentError.notNull(
'DSN is required. Use empty string to disable SDK.');
}
// if the DSN is empty, let's disable the SDK
if (options.dsn.isEmpty) {
close();
return false;
}

// if logger os NoOp, let's set a logger that prints on the console
if (options.debug && options.logger == noOpLogger) {
options.logger = dartLogger;
}
Expand Down
10 changes: 10 additions & 0 deletions dart/test/hub_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ void main() {
final returnedId = await hub.captureEvent(event);
expect(eventId.toString(), returnedId.toString());
});

test('should install integrations', () {
options.addIntegration(integration);
final hub = Hub(options);
client = MockSentryClient();
hub.bindClient(client);
// TODO: assert that integration(hub, options) has been invoked
});
});

group('Hub scope', () {
Expand Down Expand Up @@ -183,3 +191,5 @@ void main() {
// could we set [hub.stack] as @visibleForTesting ?
});
}

void integration(Hub hub, SentryOptions options) {}