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

fix: unsupported types with expando #1690

Merged
merged 10 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixes

- Unsupported types with Expando ([#1690](https://github.com/getsentry/sentry-dart/pull/1690))

### Features

- Breadcrumbs for file I/O operations ([#1649](https://github.com/getsentry/sentry-dart/pull/1649))
Expand Down
36 changes: 36 additions & 0 deletions dart/lib/src/hub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,8 @@ class _WeakMap {

final SentryOptions _options;

final throwableHandler = UnsupportedThrowablesHandler();

_WeakMap(this._options);

void add(
Expand All @@ -599,6 +601,7 @@ class _WeakMap {
if (throwable == null) {
return;
}
throwable = throwableHandler.wrapIfUnsupportedType(throwable);
try {
if (_expando[throwable] == null) {
_expando[throwable] = MapEntry(span, transaction);
Expand All @@ -617,6 +620,7 @@ class _WeakMap {
if (throwable == null) {
return null;
}
throwable = throwableHandler.wrapIfUnsupportedType(throwable);
try {
return _expando[throwable] as MapEntry<ISentrySpan, String>?;
} catch (exception, stackTrace) {
Expand All @@ -630,3 +634,35 @@ class _WeakMap {
return null;
}
}

/// A handler for unsupported throwables used for Expando<Object>.
@visibleForTesting
class UnsupportedThrowablesHandler {
final _unsupportedTypes = {String, int, double, bool};
final _unsupportedThrowables = <Object>{};

dynamic wrapIfUnsupportedType(dynamic throwable) {
if (_unsupportedTypes.contains(throwable.runtimeType)) {
throwable = _UnsupportedExceptionWrapper(Exception(throwable));
_unsupportedThrowables.add(throwable);
}
return _unsupportedThrowables.lookup(throwable) ?? throwable;
}
}

class _UnsupportedExceptionWrapper {
_UnsupportedExceptionWrapper(this.exception);

final Exception exception;

@override
bool operator ==(Object other) {
if (other is _UnsupportedExceptionWrapper) {
return other.exception.toString() == exception.toString();
}
return false;
}

@override
int get hashCode => exception.toString().hashCode;
}
4 changes: 2 additions & 2 deletions dart/test/hub_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ void main() {

final capturedEvent = fixture.client.captureEventCalls.first;

expect(capturedEvent.event.transaction, isNull);
expect(capturedEvent.event.contexts.trace, isNull);
expect(capturedEvent.event.transaction, 'test');
expect(capturedEvent.event.contexts.trace, isNotNull);
});
});

Expand Down
92 changes: 92 additions & 0 deletions dart/test/unsupported_throwables_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import 'package:sentry/src/hub.dart';
import 'package:test/expect.dart';
import 'package:test/scaffolding.dart';

void main() {
late Fixture fixture;

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

group('unsupported throwable types', () {
test('wrapped string throwable does not throw when expanding', () async {
final throwableHandler = fixture.sut;
final unsupportedThrowable = 'test throwable';
final wrappedThrowable =
throwableHandler.wrapIfUnsupportedType(unsupportedThrowable);

expect(() {
fixture.expando[wrappedThrowable];
}, returnsNormally);
});

test('wrapped int throwable does not throw when expanding', () async {
final throwableHandler = fixture.sut;
final unsupportedThrowable = 1;
final wrappedThrowable =
throwableHandler.wrapIfUnsupportedType(unsupportedThrowable);

expect(() {
fixture.expando[wrappedThrowable];
}, returnsNormally);
});

test('wrapped double throwable does not throw when expanding', () async {
final throwableHandler = fixture.sut;
final unsupportedThrowable = 1.0;
final wrappedThrowable =
throwableHandler.wrapIfUnsupportedType(unsupportedThrowable);

expect(() {
fixture.expando[wrappedThrowable];
}, returnsNormally);
});

test('wrapped bool throwable does not throw when expanding', () async {
final throwableHandler = fixture.sut;
final unsupportedThrowable = true;
final wrappedThrowable =
throwableHandler.wrapIfUnsupportedType(unsupportedThrowable);

expect(() {
fixture.expando[wrappedThrowable];
}, returnsNormally);
});

test(
'creating multiple instances of string wrapped exceptions accesses the same expando value',
() async {
final unsupportedThrowable = 'test throwable';
final throwableHandler = fixture.sut;

final first =
throwableHandler.wrapIfUnsupportedType(unsupportedThrowable);
fixture.expando[first] = 1;

final second =
throwableHandler.wrapIfUnsupportedType(unsupportedThrowable);
expect(fixture.expando[second], 1);
fixture.expando[second] = 2.0;

final third =
throwableHandler.wrapIfUnsupportedType(unsupportedThrowable);
expect(fixture.expando[third], 2.0);
});
});

group('supported throwable type', () {
test('does not wrap exception if it is a supported type', () async {
final supportedThrowable = Exception('test throwable');
final result = fixture.sut.wrapIfUnsupportedType(supportedThrowable);

expect(result, supportedThrowable);
});
});
}

class Fixture {
final expando = Expando();

UnsupportedThrowablesHandler get sut => UnsupportedThrowablesHandler();
}
2 changes: 1 addition & 1 deletion dio/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version: 7.10.1
homepage: https://docs.sentry.io/platforms/dart/
repository: https://github.com/getsentry/sentry-dart
issue_tracker: https://github.com/getsentry/sentry-dart/issues
documentation: https://docs.sentry.io/platforms/dart/configuration/integrations/dio/
documentation: https://docs.sentry.io/platforms/dart/integrations/dio/

environment:
sdk: '>=2.17.0 <4.0.0'
Expand Down
2 changes: 1 addition & 1 deletion logging/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version: 7.10.1
homepage: https://docs.sentry.io/platforms/dart/
repository: https://github.com/getsentry/sentry-dart
issue_tracker: https://github.com/getsentry/sentry-dart/issues
documentation: https://docs.sentry.io/platforms/dart/configuration/integrations/logging/
documentation: https://docs.sentry.io/platforms/dart/integrations/logging/

environment:
sdk: '>=2.17.0 <4.0.0'
Expand Down