|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import '../../protocol.dart'; |
| 4 | +import '../../sentry_options.dart'; |
| 5 | +import 'exception_event_processor.dart'; |
| 6 | + |
| 7 | +ExceptionEventProcessor exceptionEventProcessor(SentryOptions options) => |
| 8 | + IoExceptionEventProcessor(options); |
| 9 | + |
| 10 | +class IoExceptionEventProcessor implements ExceptionEventProcessor { |
| 11 | + IoExceptionEventProcessor(this._options); |
| 12 | + |
| 13 | + final SentryOptions _options; |
| 14 | + |
| 15 | + @override |
| 16 | + SentryEvent apply(SentryEvent event, {dynamic hint}) { |
| 17 | + final throwable = event.throwable; |
| 18 | + if (throwable is HttpException) { |
| 19 | + return _applyHttpException(throwable, event); |
| 20 | + } |
| 21 | + if (throwable is SocketException) { |
| 22 | + return _applySocketException(throwable, event); |
| 23 | + } |
| 24 | + if (throwable is FileSystemException) { |
| 25 | + return _applyFileSystemException(throwable, event); |
| 26 | + } |
| 27 | + |
| 28 | + return event; |
| 29 | + } |
| 30 | + |
| 31 | + // https://api.dart.dev/stable/dart-io/HttpException-class.html |
| 32 | + SentryEvent _applyHttpException(HttpException exception, SentryEvent event) { |
| 33 | + final uri = exception.uri; |
| 34 | + if (uri == null) { |
| 35 | + return event; |
| 36 | + } |
| 37 | + return event.copyWith( |
| 38 | + request: event.request ?? SentryRequest.fromUri(uri: uri), |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + // https://api.dart.dev/stable/dart-io/SocketException-class.html |
| 43 | + SentryEvent _applySocketException( |
| 44 | + SocketException exception, |
| 45 | + SentryEvent event, |
| 46 | + ) { |
| 47 | + final address = exception.address; |
| 48 | + final osError = exception.osError; |
| 49 | + if (address == null) { |
| 50 | + return event.copyWith( |
| 51 | + exceptions: [ |
| 52 | + // OSError is the underlying error |
| 53 | + // https://api.dart.dev/stable/dart-io/SocketException/osError.html |
| 54 | + // https://api.dart.dev/stable/dart-io/OSError-class.html |
| 55 | + if (osError != null) _sentryExceptionfromOsError(osError), |
| 56 | + ...?event.exceptions, |
| 57 | + ], |
| 58 | + ); |
| 59 | + } |
| 60 | + SentryRequest? request; |
| 61 | + try { |
| 62 | + var uri = Uri.parse(address.host); |
| 63 | + request = SentryRequest.fromUri(uri: uri); |
| 64 | + } catch (exception, stackTrace) { |
| 65 | + _options.logger( |
| 66 | + SentryLevel.error, |
| 67 | + 'Could not parse ${address.host} to Uri', |
| 68 | + exception: exception, |
| 69 | + stackTrace: stackTrace, |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + return event.copyWith( |
| 74 | + request: event.request ?? request, |
| 75 | + exceptions: [ |
| 76 | + // OSError is the underlying error |
| 77 | + // https://api.dart.dev/stable/dart-io/SocketException/osError.html |
| 78 | + // https://api.dart.dev/stable/dart-io/OSError-class.html |
| 79 | + if (osError != null) _sentryExceptionfromOsError(osError), |
| 80 | + ...?event.exceptions, |
| 81 | + ], |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + // https://api.dart.dev/stable/dart-io/FileSystemException-class.html |
| 86 | + SentryEvent _applyFileSystemException( |
| 87 | + FileSystemException exception, |
| 88 | + SentryEvent event, |
| 89 | + ) { |
| 90 | + final osError = exception.osError; |
| 91 | + return event.copyWith( |
| 92 | + exceptions: [ |
| 93 | + // OSError is the underlying error |
| 94 | + // https://api.dart.dev/stable/dart-io/FileSystemException/osError.html |
| 95 | + // https://api.dart.dev/stable/dart-io/OSError-class.html |
| 96 | + if (osError != null) _sentryExceptionfromOsError(osError), |
| 97 | + ...?event.exceptions, |
| 98 | + ], |
| 99 | + ); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +SentryException _sentryExceptionfromOsError(OSError osError) { |
| 104 | + return SentryException( |
| 105 | + type: osError.runtimeType.toString(), |
| 106 | + value: osError.toString(), |
| 107 | + // osError.errorCode is likely a posix signal |
| 108 | + // https://develop.sentry.dev/sdk/event-payloads/types/#mechanismmeta |
| 109 | + mechanism: Mechanism( |
| 110 | + type: 'OSError', |
| 111 | + meta: { |
| 112 | + 'errno': {'number': osError.errorCode}, |
| 113 | + }, |
| 114 | + ), |
| 115 | + ); |
| 116 | +} |
0 commit comments