Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature : static SDK main entry withSentry.init(options),... #108

Merged
merged 18 commits into from
Oct 14, 2020
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
26 changes: 12 additions & 14 deletions dart/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Future<void> main(List<String> rawArgs) async {
}

final dsn = rawArgs.single;
Sentry.init(SentryOptions(dsn: dsn));
Sentry.init(Options(dsn: dsn));

print('\nReporting a complete event example: ');

Expand Down Expand Up @@ -51,19 +51,6 @@ Future<void> main(List<String> rawArgs) async {
}

/* TODO(rxlabz) Sentry CaptureMessage(message, level) */

/*Future<void> captureCompleteExampleEvent(SentryClient client) async {


final response = await client.captureEvent(event: event);

print('\nReporting a complete event example: ');
if (response.isSuccessful) {
print('SUCCESS\nid: ${response.eventId}');
} else {
print('FAILURE: ${response.error}');
}
*/
}

Future<void> foo() async {
Expand All @@ -77,3 +64,14 @@ Future<void> bar() async {
Future<void> baz() async {
throw StateError('This is a test error');
}

class Options implements OptionsConfiguration<SentryOptions> {
final String dsn;

Options({this.dsn});

@override
void configure(SentryOptions options) {
options.dsn = dsn;
}
}
21 changes: 11 additions & 10 deletions dart/lib/src/sentry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:meta/meta.dart';
import 'client.dart';
import 'protocol.dart';
import 'sentry_options.dart';
import 'stack_trace.dart';

/// Sentry SDK main entry point
///
Expand All @@ -12,7 +11,9 @@ class Sentry {

Sentry._();

static void init(SentryOptions options) {
static void init(OptionsConfiguration<SentryOptions> optionsConfiguration) {
final options = SentryOptions();
optionsConfiguration.configure(options);
_client = SentryClient(
dsn: options.dsn,
environmentAttributes: options.environmentAttributes,
Expand All @@ -24,14 +25,8 @@ class Sentry {
}

/// Reports an [event] to Sentry.io.
static Future<SentryResponse> captureEvent(
Event event, {
StackFrameFilter stackFrameFilter,
}) async {
return _client.captureEvent(
event: event,
stackFrameFilter: stackFrameFilter,
);
static Future<SentryResponse> captureEvent(Event event) async {
return _client.captureEvent(event: event);
}

/// Reports the [exception] and optionally its [stackTrace] to Sentry.io.
Expand All @@ -52,3 +47,9 @@ class Sentry {
@visibleForTesting
static void initClient(SentryClient client) => _client = client;
}

/// Configuration options callback
abstract class OptionsConfiguration<T extends SentryOptions> {
///configure the options
void configure(T options);
}
17 changes: 8 additions & 9 deletions dart/lib/src/sentry_options.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:http/http.dart';
import 'package:meta/meta.dart';

import 'protocol.dart';
import 'utils.dart';
Expand All @@ -11,7 +10,7 @@ class SentryOptions {

/// The DSN tells the SDK where to send the events to. If this value is not provided, the SDK will
/// just not send any events.
final String dsn;
String dsn;

/// Contains [Event] attributes that are automatically mixed into all events
/// captured through this client.
Expand All @@ -20,31 +19,31 @@ class SentryOptions {
/// event to event, such as local operating system version, the version of
/// Dart/Flutter SDK, etc. These attributes have lower precedence than those
/// supplied in the even passed to [capture].
final Event environmentAttributes;
Event environmentAttributes;

/// If [compressPayload] is `true` the outgoing HTTP payloads are compressed
/// using gzip. Otherwise, the payloads are sent in plain UTF8-encoded JSON
/// text. If not specified, the compression is enabled by default.
final bool compressPayload;
bool compressPayload;

/// If [httpClient] is provided, it is used instead of the default client to
/// make HTTP calls to Sentry.io. This is useful in tests.
final Client httpClient;
Client 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].
/// This parameter is dynamic to maintain backwards compatibility with
/// previous use of [Clock](https://pub.dartlang.org/documentation/quiver/latest/quiver.time/Clock-class.html)
/// from [`package:quiver`](https://pub.dartlang.org/packages/quiver).
final dynamic clock;
dynamic clock;

/// If [uuidGenerator] is provided, it is used to generate the "event_id"
/// field instead of the built-in random UUID v4 generator. This is useful in
/// tests.
final UuidGenerator uuidGenerator;
UuidGenerator uuidGenerator;

const SentryOptions({
@required this.dsn,
SentryOptions({
this.dsn,
this.environmentAttributes,
this.compressPayload,
this.httpClient,
Expand Down
2 changes: 1 addition & 1 deletion dart/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description: >
homepage: https://github.com/getsentry/sentry-dart

environment:
sdk: ^2.1.0
sdk: ^2.0.0

dependencies:
http: ^0.12.0
Expand Down
20 changes: 18 additions & 2 deletions dart/test/sentry_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:sentry/src/sentry_options.dart';
import 'package:test/test.dart';

void main() {
group('Sentry.init', () {
group('Sentry static entry', () {
SentryClient client;

Exception anException;
Expand All @@ -15,7 +15,7 @@ void main() {
'https://[email protected]/5428562';

setUp(() {
Sentry.init(SentryOptions(dsn: dns));
Sentry.init(Options(dsn: dns));

client = MockSentryClient();
Sentry.initClient(client);
Expand All @@ -26,6 +26,11 @@ void main() {
verify(client.captureEvent(event: event)).called(1);
});

test('should capture the event', () {
Sentry.captureEvent(event);
verify(client.captureEvent(event: event)).called(1);
});

test('should capture the exception', () {
Sentry.captureException(anException);
verify(client.captureException(exception: anException)).called(1);
Expand All @@ -35,6 +40,17 @@ void main() {

class MockSentryClient extends Mock implements SentryClient {}

class Options implements OptionsConfiguration<SentryOptions> {
final String dsn;

Options({this.dsn});

@override
void configure(SentryOptions options) {
options.dsn = dsn;
}
}

final event = Event(
loggerName: 'main',
serverName: 'server.dart',
Expand Down