Skip to content

Commit

Permalink
Merge branch 'main' into chore/improve-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Apr 25, 2023
2 parents c74e609 + d85ffe7 commit 60b103e
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ Sentry.configureScope(
```

- Add processor count to device info ([#1402](https://github.com/getsentry/sentry-dart/pull/1402))
- Add attachments to `Hint` ([#1404](https://github.com/getsentry/sentry-dart/pull/1404))

```dart
import 'dart:convert';
options.beforeSend = (event, {hint}) {
final text = 'This event should not be sent happen in prod. Investigate.';
final textAttachment = SentryAttachment.fromIntList(
utf8.encode(text),
'event_info.txt',
contentType: 'text/plain',
);
hint?.attachments.add(textAttachment);
return event;
};
```

### Fixes

Expand Down
33 changes: 33 additions & 0 deletions dart/lib/src/hint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,48 @@ import 'sentry_attachment/sentry_attachment.dart';
/// };
/// }
/// ```
///
/// The [Hint] can also be used to add attachments to events.
///
/// Example:
///
/// ```dart
/// import 'dart:convert';
///
/// options.beforeSend = (event, {hint}) {
/// final text = 'This event should not be sent happen in prod. Investigate.';
/// final textAttachment = SentryAttachment.fromIntList(
/// utf8.encode(text),
/// 'event_info.txt',
/// contentType: 'text/plain',
/// );
/// hint?.attachments.add(textAttachment);
/// return event;
/// };
/// ```
class Hint {
final Map<String, Object> _internalStorage = {};

final List<SentryAttachment> attachments = [];

SentryAttachment? screenshot;

SentryAttachment? viewHierarchy;

Hint();

factory Hint.withAttachment(SentryAttachment attachment) {
final hint = Hint();
hint.attachments.add(attachment);
return hint;
}

factory Hint.withAttachments(List<SentryAttachment> attachments) {
final hint = Hint();
hint.attachments.addAll(attachments);
return hint;
}

factory Hint.withMap(Map<String, Object> map) {
final hint = Hint();
hint.addAll(map);
Expand Down
1 change: 1 addition & 0 deletions dart/lib/src/sentry_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class SentryClient {
preparedEvent = _eventWithoutBreadcrumbsIfNeeded(preparedEvent);

var attachments = List<SentryAttachment>.from(scope?.attachments ?? []);
attachments.addAll(hint.attachments);
var screenshot = hint.screenshot;
if (screenshot != null) {
attachments.add(screenshot);
Expand Down
28 changes: 28 additions & 0 deletions dart/test/hint_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import 'package:sentry/src/hint.dart';
import 'package:sentry/src/sentry_attachment/sentry_attachment.dart';
import 'package:test/test.dart';

void main() {
late Fixture fixture;

setUp(() {
fixture = Fixture();
});

test('Hint init with map', () {
final hint = Hint.withMap({'fixture-key': 'fixture-value'});
expect("fixture-value", hint.get("fixture-key"));
Expand Down Expand Up @@ -60,4 +67,25 @@ void main() {
expect(hint.get("hint1"), null);
expect(hint.get("hint2"), null);
});

test('clear does not remove attachments, screenshot & viewHierarchy', () {
final attachment = SentryAttachment.fromIntList([], "fixture-fileName");

final sut = fixture.givenSut();
sut.attachments.add(attachment);
sut.screenshot = attachment;
sut.viewHierarchy = attachment;

sut.clear();

expect(sut.attachments.contains(attachment), true);
expect(sut.screenshot, attachment);
expect(sut.viewHierarchy, attachment);
});
}

class Fixture {
Hint givenSut() {
return Hint();
}
}
14 changes: 14 additions & 0 deletions dart/test/sentry_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1413,6 +1413,20 @@ void main() {
expect(envelope.header.traceContext, isNotNull);
});

test('captureEvent adds attachments from hint', () async {
final attachment = SentryAttachment.fromIntList([], "fixture-fileName");
final hint = Hint.withAttachment(attachment);

final sut = fixture.getSut();
await sut.captureEvent(fakeEvent, hint: hint);

final capturedEnvelope = (fixture.transport).envelopes.first;
final attachmentItem = capturedEnvelope.items.firstWhereOrNull(
(element) => element.header.type == SentryItemType.attachment);
expect(attachmentItem?.header.attachmentType,
SentryAttachment.typeAttachmentDefault);
});

test('captureEvent adds screenshot from hint', () async {
final client = fixture.getSut();
final screenshot =
Expand Down

0 comments on commit 60b103e

Please sign in to comment.