Skip to content

Commit d85ffe7

Browse files
authored
Add attachments to Hint (#1404)
1 parent 9949058 commit d85ffe7

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@
1313
- Sync `connectionTimeout` and `readTimeout` to Android ([#1397](https://github.com/getsentry/sentry-dart/pull/1397))
1414
- Set User `name` and `geo` in native plugins ([#1393](https://github.com/getsentry/sentry-dart/pull/1393))
1515
- Add processor count to device info ([#1402](https://github.com/getsentry/sentry-dart/pull/1402))
16+
- Add attachments to `Hint` ([#1404](https://github.com/getsentry/sentry-dart/pull/1404))
17+
18+
```dart
19+
import 'dart:convert';
20+
21+
options.beforeSend = (event, {hint}) {
22+
final text = 'This event should not be sent happen in prod. Investigate.';
23+
final textAttachment = SentryAttachment.fromIntList(
24+
utf8.encode(text),
25+
'event_info.txt',
26+
contentType: 'text/plain',
27+
);
28+
hint?.attachments.add(textAttachment);
29+
return event;
30+
};
31+
```
1632

1733
### Fixes
1834

dart/lib/src/hint.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,48 @@ import 'sentry_attachment/sentry_attachment.dart';
2020
/// };
2121
/// }
2222
/// ```
23+
///
24+
/// The [Hint] can also be used to add attachments to events.
25+
///
26+
/// Example:
27+
///
28+
/// ```dart
29+
/// import 'dart:convert';
30+
///
31+
/// options.beforeSend = (event, {hint}) {
32+
/// final text = 'This event should not be sent happen in prod. Investigate.';
33+
/// final textAttachment = SentryAttachment.fromIntList(
34+
/// utf8.encode(text),
35+
/// 'event_info.txt',
36+
/// contentType: 'text/plain',
37+
/// );
38+
/// hint?.attachments.add(textAttachment);
39+
/// return event;
40+
/// };
41+
/// ```
2342
class Hint {
2443
final Map<String, Object> _internalStorage = {};
2544

45+
final List<SentryAttachment> attachments = [];
46+
2647
SentryAttachment? screenshot;
2748

2849
SentryAttachment? viewHierarchy;
2950

3051
Hint();
3152

53+
factory Hint.withAttachment(SentryAttachment attachment) {
54+
final hint = Hint();
55+
hint.attachments.add(attachment);
56+
return hint;
57+
}
58+
59+
factory Hint.withAttachments(List<SentryAttachment> attachments) {
60+
final hint = Hint();
61+
hint.attachments.addAll(attachments);
62+
return hint;
63+
}
64+
3265
factory Hint.withMap(Map<String, Object> map) {
3366
final hint = Hint();
3467
hint.addAll(map);

dart/lib/src/sentry_client.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class SentryClient {
111111
preparedEvent = _eventWithoutBreadcrumbsIfNeeded(preparedEvent);
112112

113113
var attachments = List<SentryAttachment>.from(scope?.attachments ?? []);
114+
attachments.addAll(hint.attachments);
114115
var screenshot = hint.screenshot;
115116
if (screenshot != null) {
116117
attachments.add(screenshot);

dart/test/hint_test.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import 'package:sentry/src/hint.dart';
2+
import 'package:sentry/src/sentry_attachment/sentry_attachment.dart';
23
import 'package:test/test.dart';
34

45
void main() {
6+
late Fixture fixture;
7+
8+
setUp(() {
9+
fixture = Fixture();
10+
});
11+
512
test('Hint init with map', () {
613
final hint = Hint.withMap({'fixture-key': 'fixture-value'});
714
expect("fixture-value", hint.get("fixture-key"));
@@ -60,4 +67,25 @@ void main() {
6067
expect(hint.get("hint1"), null);
6168
expect(hint.get("hint2"), null);
6269
});
70+
71+
test('clear does not remove attachments, screenshot & viewHierarchy', () {
72+
final attachment = SentryAttachment.fromIntList([], "fixture-fileName");
73+
74+
final sut = fixture.givenSut();
75+
sut.attachments.add(attachment);
76+
sut.screenshot = attachment;
77+
sut.viewHierarchy = attachment;
78+
79+
sut.clear();
80+
81+
expect(sut.attachments.contains(attachment), true);
82+
expect(sut.screenshot, attachment);
83+
expect(sut.viewHierarchy, attachment);
84+
});
85+
}
86+
87+
class Fixture {
88+
Hint givenSut() {
89+
return Hint();
90+
}
6391
}

dart/test/sentry_client_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,20 @@ void main() {
14131413
expect(envelope.header.traceContext, isNotNull);
14141414
});
14151415

1416+
test('captureEvent adds attachments from hint', () async {
1417+
final attachment = SentryAttachment.fromIntList([], "fixture-fileName");
1418+
final hint = Hint.withAttachment(attachment);
1419+
1420+
final sut = fixture.getSut();
1421+
await sut.captureEvent(fakeEvent, hint: hint);
1422+
1423+
final capturedEnvelope = (fixture.transport).envelopes.first;
1424+
final attachmentItem = capturedEnvelope.items.firstWhereOrNull(
1425+
(element) => element.header.type == SentryItemType.attachment);
1426+
expect(attachmentItem?.header.attachmentType,
1427+
SentryAttachment.typeAttachmentDefault);
1428+
});
1429+
14161430
test('captureEvent adds screenshot from hint', () async {
14171431
final client = fixture.getSut();
14181432
final screenshot =

0 commit comments

Comments
 (0)