|
| 1 | +import 'package:mockito/mockito.dart'; |
| 2 | +import 'package:sentry/sentry.dart'; |
| 3 | +import 'package:sentry/src/dart_exception_type_identifier.dart'; |
| 4 | +import 'package:sentry/src/sentry_exception_factory.dart'; |
| 5 | +import 'package:test/test.dart'; |
| 6 | + |
| 7 | +import 'mocks.dart'; |
| 8 | +import 'mocks.mocks.dart'; |
| 9 | +import 'mocks/mock_transport.dart'; |
| 10 | +import 'sentry_client_test.dart'; |
| 11 | + |
| 12 | +void main() { |
| 13 | + late Fixture fixture; |
| 14 | + |
| 15 | + setUp(() { |
| 16 | + fixture = Fixture(); |
| 17 | + }); |
| 18 | + |
| 19 | + group('ExceptionTypeIdentifiers', () { |
| 20 | + test('first in the list will be processed first', () { |
| 21 | + fixture.options |
| 22 | + .prependExceptionTypeIdentifier(DartExceptionTypeIdentifier()); |
| 23 | + fixture.options |
| 24 | + .prependExceptionTypeIdentifier(ObfuscatedExceptionIdentifier()); |
| 25 | + |
| 26 | + final factory = SentryExceptionFactory(fixture.options); |
| 27 | + final sentryException = factory.getSentryException(ObfuscatedException()); |
| 28 | + |
| 29 | + expect(sentryException.type, equals('ObfuscatedException')); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + group('CachingExceptionTypeIdentifier', () { |
| 34 | + late MockExceptionTypeIdentifier mockIdentifier; |
| 35 | + late CachingExceptionTypeIdentifier cachingIdentifier; |
| 36 | + |
| 37 | + setUp(() { |
| 38 | + mockIdentifier = MockExceptionTypeIdentifier(); |
| 39 | + cachingIdentifier = CachingExceptionTypeIdentifier(mockIdentifier); |
| 40 | + }); |
| 41 | + |
| 42 | + test('should return cached result for known types', () { |
| 43 | + final exception = Exception('Test'); |
| 44 | + when(mockIdentifier.identifyType(exception)).thenReturn('TestException'); |
| 45 | + |
| 46 | + expect( |
| 47 | + cachingIdentifier.identifyType(exception), equals('TestException')); |
| 48 | + expect( |
| 49 | + cachingIdentifier.identifyType(exception), equals('TestException')); |
| 50 | + expect( |
| 51 | + cachingIdentifier.identifyType(exception), equals('TestException')); |
| 52 | + |
| 53 | + verify(mockIdentifier.identifyType(exception)).called(1); |
| 54 | + }); |
| 55 | + |
| 56 | + test('should not cache unknown types', () { |
| 57 | + final exception = ObfuscatedException(); |
| 58 | + |
| 59 | + when(mockIdentifier.identifyType(exception)).thenReturn(null); |
| 60 | + |
| 61 | + expect(cachingIdentifier.identifyType(exception), isNull); |
| 62 | + expect(cachingIdentifier.identifyType(exception), isNull); |
| 63 | + expect(cachingIdentifier.identifyType(exception), isNull); |
| 64 | + |
| 65 | + verify(mockIdentifier.identifyType(exception)).called(3); |
| 66 | + }); |
| 67 | + |
| 68 | + test('should return null for unknown exception type', () { |
| 69 | + final exception = Exception('Unknown'); |
| 70 | + when(mockIdentifier.identifyType(exception)).thenReturn(null); |
| 71 | + |
| 72 | + expect(cachingIdentifier.identifyType(exception), isNull); |
| 73 | + }); |
| 74 | + |
| 75 | + test('should handle different exception types separately', () { |
| 76 | + final exception1 = Exception('Test1'); |
| 77 | + final exception2 = FormatException('Test2'); |
| 78 | + |
| 79 | + when(mockIdentifier.identifyType(exception1)).thenReturn('Exception'); |
| 80 | + when(mockIdentifier.identifyType(exception2)) |
| 81 | + .thenReturn('FormatException'); |
| 82 | + |
| 83 | + expect(cachingIdentifier.identifyType(exception1), equals('Exception')); |
| 84 | + expect(cachingIdentifier.identifyType(exception2), |
| 85 | + equals('FormatException')); |
| 86 | + |
| 87 | + // Call again to test caching |
| 88 | + expect(cachingIdentifier.identifyType(exception1), equals('Exception')); |
| 89 | + expect(cachingIdentifier.identifyType(exception2), |
| 90 | + equals('FormatException')); |
| 91 | + |
| 92 | + verify(mockIdentifier.identifyType(exception1)).called(1); |
| 93 | + verify(mockIdentifier.identifyType(exception2)).called(1); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + group('Integration test', () { |
| 98 | + setUp(() { |
| 99 | + fixture.options.transport = MockTransport(); |
| 100 | + }); |
| 101 | + |
| 102 | + test( |
| 103 | + 'should capture CustomException as exception type with custom identifier', |
| 104 | + () async { |
| 105 | + fixture.options |
| 106 | + .prependExceptionTypeIdentifier(ObfuscatedExceptionIdentifier()); |
| 107 | + |
| 108 | + final client = SentryClient(fixture.options); |
| 109 | + |
| 110 | + await client.captureException(ObfuscatedException()); |
| 111 | + |
| 112 | + final transport = fixture.options.transport as MockTransport; |
| 113 | + final capturedEnvelope = transport.envelopes.first; |
| 114 | + final capturedEvent = await eventFromEnvelope(capturedEnvelope); |
| 115 | + |
| 116 | + expect( |
| 117 | + capturedEvent.exceptions!.first.type, equals('ObfuscatedException')); |
| 118 | + }); |
| 119 | + |
| 120 | + test( |
| 121 | + 'should capture PlaceHolderException as exception type without custom identifier', |
| 122 | + () async { |
| 123 | + final client = SentryClient(fixture.options); |
| 124 | + |
| 125 | + await client.captureException(ObfuscatedException()); |
| 126 | + |
| 127 | + final transport = fixture.options.transport as MockTransport; |
| 128 | + final capturedEnvelope = transport.envelopes.first; |
| 129 | + final capturedEvent = await eventFromEnvelope(capturedEnvelope); |
| 130 | + |
| 131 | + expect( |
| 132 | + capturedEvent.exceptions!.first.type, equals('PlaceHolderException')); |
| 133 | + }); |
| 134 | + }); |
| 135 | +} |
| 136 | + |
| 137 | +class Fixture { |
| 138 | + SentryOptions options = SentryOptions(dsn: fakeDsn); |
| 139 | +} |
| 140 | + |
| 141 | +// We use this PlaceHolder exception to mimic an obfuscated runtimeType |
| 142 | +class PlaceHolderException implements Exception {} |
| 143 | + |
| 144 | +class ObfuscatedException implements Exception { |
| 145 | + @override |
| 146 | + Type get runtimeType => PlaceHolderException; |
| 147 | +} |
| 148 | + |
| 149 | +class ObfuscatedExceptionIdentifier implements ExceptionTypeIdentifier { |
| 150 | + @override |
| 151 | + String? identifyType(dynamic throwable) { |
| 152 | + if (throwable is ObfuscatedException) return 'ObfuscatedException'; |
| 153 | + return null; |
| 154 | + } |
| 155 | +} |
0 commit comments