Skip to content

Commit b1761f4

Browse files
authored
unified api fixes and add web example (#137)
1 parent 7b81890 commit b1761f4

20 files changed

+588
-86
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ ios/
1515
build/
1616
.cxx/
1717

18+
1819
.test_coverage.dart
20+
dart/coverage/*

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
- Ref: execute before send callback
3636
- Feat: add lastEventId to the Sentry static API
3737
- Feat: addBreadcrumb on Static API
38+
- Add a Dart web example
3839
- Fix: Integrations are executed on Hub creation
3940
- Fix: NoOp encode for Web
4041
- Fix: Breadcrumb data should accept serializable types and not only String values

dart/example/event_example.dart

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,45 @@ final event = SentryEvent(
66
release: '1.4.0-preview.1',
77
environment: 'Test',
88
message: Message('This is an example Dart event.'),
9-
transaction: '/example/app',
10-
level: SentryLevel.warning,
119
tags: const <String, String>{'project-id': '7371'},
12-
extra: const <String, String>{'company-name': 'Dart Inc'},
10+
extra: const <String, String>{'section': '1'},
1311
fingerprint: const <String>['example-dart'],
1412
userContext: const User(
15-
id: '800',
16-
username: 'first-user',
17-
18-
ipAddress: '127.0.0.1',
19-
extras: <String, String>{'first-sign-in': '2020-01-01'}),
13+
id: '800',
14+
username: 'first-user',
15+
16+
ipAddress: '127.0.0.1',
17+
extras: <String, String>{'first-sign-in': '2020-01-01'},
18+
),
2019
breadcrumbs: [
2120
Breadcrumb(
22-
message: 'UI Lifecycle',
23-
timestamp: DateTime.now().toUtc(),
24-
category: 'ui.lifecycle',
25-
type: 'navigation',
26-
data: {'screen': 'MainActivity', 'state': 'created'},
27-
level: SentryLevel.info)
21+
message: 'UI Lifecycle',
22+
timestamp: DateTime.now().toUtc(),
23+
category: 'ui.lifecycle',
24+
type: 'navigation',
25+
data: {'screen': 'MainActivity', 'state': 'created'},
26+
level: SentryLevel.info,
27+
)
2828
],
2929
contexts: Contexts(
3030
operatingSystem: const OperatingSystem(
31-
name: 'Android',
32-
version: '5.0.2',
33-
build: 'LRX22G.P900XXS0BPL2',
34-
kernelVersion:
35-
'Linux version 3.4.39-5726670 (dpi@SWHC3807) (gcc version 4.8 (GCC) ) #1 SMP PREEMPT Thu Dec 1 19:42:39 KST 2016',
36-
rooted: false),
31+
name: 'Android',
32+
version: '5.0.2',
33+
build: 'LRX22G.P900XXS0BPL2',
34+
kernelVersion:
35+
'Linux version 3.4.39-5726670 (dpi@SWHC3807) (gcc version 4.8 (GCC) ) #1 SMP PREEMPT Thu Dec 1 19:42:39 KST 2016',
36+
rooted: false,
37+
),
3738
runtimes: [const Runtime(name: 'ART', version: '5')],
3839
app: App(
39-
name: 'Example Dart App',
40-
version: '1.42.0',
41-
identifier: 'HGT-App-13',
42-
build: '93785',
43-
buildType: 'release',
44-
deviceAppHash: '5afd3a6',
45-
startTime: DateTime.now().toUtc()),
40+
name: 'Example Dart App',
41+
version: '1.42.0',
42+
identifier: 'HGT-App-13',
43+
build: '93785',
44+
buildType: 'release',
45+
deviceAppHash: '5afd3a6',
46+
startTime: DateTime.now().toUtc(),
47+
),
4648
browser: const Browser(name: 'Firefox', version: '42.0.1'),
4749
device: Device(
4850
name: 'SM-P900',

dart/example/main.dart

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,72 @@
33
// found in the LICENSE file.
44

55
import 'dart:async';
6-
import 'dart:io';
76

87
import 'package:sentry/sentry.dart';
98

109
import 'event_example.dart';
1110

1211
/// Sends a test exception report to Sentry.io using this Dart client.
13-
Future<void> main(List<String> rawArgs) async {
14-
if (rawArgs.length != 1) {
15-
stderr.writeln(
16-
'Expected exactly one argument, which is the DSN issued by Sentry.io to your project.');
17-
exit(1);
18-
}
12+
Future<void> main() async {
13+
const dsn =
14+
'https://[email protected]/5428562';
15+
16+
SentryEvent processTagEvent(SentryEvent event, Object hint) =>
17+
event..tags.addAll({'page-locale': 'en-us'});
18+
19+
Sentry.init((options) => options
20+
..dsn = dsn
21+
..addEventProcessor(processTagEvent));
22+
23+
Sentry.addBreadcrumb(
24+
Breadcrumb(
25+
message: 'Authenticated user',
26+
category: 'auth',
27+
type: 'debug',
28+
data: {
29+
'admin': true,
30+
'permissions': [1, 2, 3]
31+
},
32+
),
33+
);
1934

20-
final dsn = rawArgs.single;
21-
Sentry.init((options) => options.dsn = dsn);
35+
Sentry.configureScope((scope) {
36+
scope
37+
..user = User(
38+
id: '800',
39+
username: 'first-user',
40+
41+
ipAddress: '127.0.0.1',
42+
extras: <String, String>{'first-sign-in': '2020-01-01'},
43+
)
44+
..fingerprint = ['example-dart']
45+
..transaction = '/example/app'
46+
..level = SentryLevel.warning
47+
..setTag('build', '579')
48+
..setExtra('company-name', 'Dart Inc');
49+
});
2250

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

2553
// Sends a full Sentry event payload to show the different parts of the UI.
2654
final sentryId = await Sentry.captureEvent(event);
2755

28-
print('SentryId : ${sentryId}');
56+
print('Capture event result : SentryId : ${sentryId}');
57+
58+
print('\nCapture message: ');
59+
60+
// Sends a full Sentry event payload to show the different parts of the UI.
61+
final messageSentryId = await Sentry.captureMessage(
62+
'Message 1',
63+
level: SentryLevel.warning,
64+
template: 'Message %s',
65+
params: ['1'],
66+
);
67+
68+
print('Capture message result : SentryId : ${messageSentryId}');
2969

3070
try {
31-
await foo();
71+
await loadConfig();
3272
} catch (error, stackTrace) {
3373
print('\nReporting the following stack trace: ');
3474
print(stackTrace);
@@ -37,22 +77,22 @@ Future<void> main(List<String> rawArgs) async {
3777
stackTrace: stackTrace,
3878
);
3979

40-
print('SentryId : ${sentryId}');
80+
print('Capture exception result : SentryId : ${sentryId}');
4181
} finally {
4282
await Sentry.close();
4383
}
4484

4585
/* TODO(rxlabz) Sentry CaptureMessage(message, level) */
4686
}
4787

48-
Future<void> foo() async {
49-
await bar();
88+
Future<void> loadConfig() async {
89+
await parseConfig();
5090
}
5191

52-
Future<void> bar() async {
53-
await baz();
92+
Future<void> parseConfig() async {
93+
await decode();
5494
}
5595

56-
Future<void> baz() async {
96+
Future<void> decode() async {
5797
throw StateError('This is a test error');
5898
}

dart/example_web/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Files and directories created by pub
2+
.dart_tool/
3+
.packages
4+
5+
# Conventional directory for build outputs
6+
build/
7+
8+
# Directory created by dartdoc
9+
doc/api/

dart/example_web/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Sentry Dart : web example
2+
3+
```dart
4+
pub get
5+
6+
# run the project ( see https://dart.dev/tools/webdev#serve )
7+
webdev serve --release
8+
9+
```

dart/example_web/pubspec.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: sentry_dart_web_example
2+
description: An absolute bare-bones web app.
3+
# version: 1.0.0
4+
#homepage: https://www.example.com
5+
6+
environment:
7+
sdk: ^2.0.0
8+
9+
dependencies:
10+
sentry:
11+
path: ../..
12+
13+
dev_dependencies:
14+
build_runner: ^1.10.0
15+
build_web_compilers: ^2.13.0
16+
pedantic: ^1.9.0

dart/example_web/web/event.dart

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import 'package:sentry/src/protocol.dart';
2+
3+
final event = SentryEvent(
4+
logger: 'main',
5+
serverName: 'server.dart',
6+
release: '1.4.0-preview.1',
7+
environment: 'Test',
8+
message: Message('This is an example Dart event.'),
9+
tags: const <String, String>{'project-id': '7371'},
10+
extra: const <String, String>{'section': '1'},
11+
fingerprint: const <String>['example-dart'],
12+
userContext: const User(
13+
id: '800',
14+
username: 'first-user',
15+
16+
ipAddress: '127.0.0.1',
17+
extras: <String, String>{'first-sign-in': '2020-01-01'},
18+
),
19+
breadcrumbs: [
20+
Breadcrumb(
21+
message: 'UI Lifecycle',
22+
timestamp: DateTime.now().toUtc(),
23+
category: 'ui.lifecycle',
24+
type: 'navigation',
25+
data: {'screen': 'MainActivity', 'state': 'created'},
26+
level: SentryLevel.info,
27+
)
28+
],
29+
contexts: Contexts(
30+
operatingSystem: const OperatingSystem(
31+
name: 'Android',
32+
version: '5.0.2',
33+
build: 'LRX22G.P900XXS0BPL2',
34+
kernelVersion:
35+
'Linux version 3.4.39-5726670 (dpi@SWHC3807) (gcc version 4.8 (GCC) ) #1 SMP PREEMPT Thu Dec 1 19:42:39 KST 2016',
36+
rooted: false,
37+
),
38+
runtimes: [const Runtime(name: 'ART', version: '5')],
39+
app: App(
40+
name: 'Example Dart App',
41+
version: '1.42.0',
42+
identifier: 'HGT-App-13',
43+
build: '93785',
44+
buildType: 'release',
45+
deviceAppHash: '5afd3a6',
46+
startTime: DateTime.now().toUtc(),
47+
),
48+
browser: const Browser(name: 'Firefox', version: '42.0.1'),
49+
device: Device(
50+
name: 'SM-P900',
51+
family: 'SM-P900',
52+
model: 'SM-P900 (LRX22G)',
53+
modelId: 'LRX22G',
54+
arch: 'armeabi-v7a',
55+
batteryLevel: 99,
56+
orientation: Orientation.landscape,
57+
manufacturer: 'samsung',
58+
brand: 'samsung',
59+
screenResolution: '2560x1600',
60+
screenDensity: 2.1,
61+
screenDpi: 320,
62+
online: true,
63+
charging: true,
64+
lowMemory: true,
65+
simulator: false,
66+
memorySize: 1500,
67+
freeMemory: 200,
68+
usableMemory: 4294967296,
69+
storageSize: 4294967296,
70+
freeStorage: 2147483648,
71+
externalStorageSize: 8589934592,
72+
externalFreeStorage: 2863311530,
73+
bootTime: DateTime.now().toUtc(),
74+
timezone: 'America/Toronto',
75+
),
76+
),
77+
);

dart/example_web/web/favicon.ico

3.48 KB
Binary file not shown.

dart/example_web/web/index.html

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!DOCTYPE html>
2+
3+
<html>
4+
<head>
5+
<meta charset="utf-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<meta name="scaffolded-by" content="https://github.com/dart-lang/stagehand">
9+
<title>dart_web</title>
10+
<link rel="stylesheet" href="styles.css">
11+
<link rel="icon" href="favicon.ico">
12+
13+
<style>
14+
.bloc {
15+
display: flex;
16+
margin: 1rem;
17+
}
18+
19+
.result {
20+
padding-left: 1rem;
21+
color: green;
22+
display: none;
23+
}
24+
</style>
25+
26+
<script defer src="main.dart.js"></script>
27+
</head>
28+
29+
<body>
30+
31+
<div id="output"></div>
32+
33+
<div class="bloc">
34+
<button id="btEvent">Capture Event</button>
35+
<div id="eventResult" class="result">Captured</div>
36+
</div>
37+
38+
<div class="bloc">
39+
<button id="btMessage">Capture Message</button>
40+
<div id="messageResult" class="result">Captured</div>
41+
</div>
42+
43+
<div class="bloc">
44+
<button id="btException">Capture Exception</button>
45+
<div id="exceptionResult" class="result">Captured</div>
46+
</div>
47+
48+
</body>
49+
</html>

0 commit comments

Comments
 (0)