Skip to content

Commit

Permalink
Support string, int, double and bool as throwable for expando
Browse files Browse the repository at this point in the history
  • Loading branch information
buenaflor committed Oct 19, 2023
1 parent daa1b33 commit 564d818
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 2 deletions.
37 changes: 37 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 exceptionsWrapperUtil = ExceptionWrapperUtil();

_WeakMap(this._options);

void add(
Expand All @@ -599,6 +601,8 @@ class _WeakMap {
if (throwable == null) {
return;
}
throwable = exceptionsWrapperUtil.wrapIfUnsupportedType(throwable);
_expando[throwable];
try {
if (_expando[throwable] == null) {
_expando[throwable] = MapEntry(span, transaction);
Expand All @@ -617,6 +621,7 @@ class _WeakMap {
if (throwable == null) {
return null;
}
throwable = exceptionsWrapperUtil.wrapIfUnsupportedType(throwable);
try {
return _expando[throwable] as MapEntry<ISentrySpan, String>?;
} catch (exception, stackTrace) {
Expand All @@ -626,7 +631,39 @@ class _WeakMap {
exception: exception,
stackTrace: stackTrace,
);
print('hahaa');
}
return null;
}
}

class ExceptionWrapperUtil {
final _unsupportedTypes = {String, int, double, bool};
final _unsupportedThrowables = <Object>{};

/// Wraps the throwable as [_ExceptionWrapper] if it is of an unsupported type.
dynamic wrapIfUnsupportedType(dynamic throwable) {
if (_unsupportedTypes.contains(throwable.runtimeType)) {
throwable = _ExceptionWrapper(Exception(throwable));
_unsupportedThrowables.add(throwable);
}
return _unsupportedThrowables.lookup(throwable) ?? throwable;
}
}

class _ExceptionWrapper {
_ExceptionWrapper(this.exception);

final Exception exception;

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

@override
int get hashCode => exception.toString().hashCode;
}
6 changes: 4 additions & 2 deletions dart/test/hub_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:ffi';

import 'package:collection/collection.dart';
import 'package:sentry/sentry.dart';
import 'package:sentry/src/client_reports/discard_reason.dart';
Expand Down Expand Up @@ -148,8 +150,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/wrapper_exception_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 exceptionWrapperUtil = fixture.sut;
final unsupportedThrowable = 'test throwable';
final wrappedThrowable =
exceptionWrapperUtil.wrapIfUnsupportedType(unsupportedThrowable);

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

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

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

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

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

test('wrapped bool throwable does not throw when expanding', () async {
final exceptionWrapperUtil = fixture.sut;
final unsupportedThrowable = true;
final wrappedThrowable =
exceptionWrapperUtil.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 exceptionWrapperUtil = fixture.sut;

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

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

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

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();

ExceptionWrapperUtil get sut => ExceptionWrapperUtil();
}

0 comments on commit 564d818

Please sign in to comment.