|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:meta/meta.dart'; |
| 4 | + |
| 5 | +import '../sentry.dart'; |
| 6 | + |
| 7 | +@internal |
| 8 | +class SentryRunZonedGuarded { |
| 9 | + /// Needed to check if we somehow caused a `print()` recursion |
| 10 | + static var _isPrinting = false; |
| 11 | + |
| 12 | + static R? sentryRunZonedGuarded<R>( |
| 13 | + Hub hub, |
| 14 | + R Function() body, |
| 15 | + void Function(Object error, StackTrace stack)? onError, { |
| 16 | + Map<Object?, Object?>? zoneValues, |
| 17 | + ZoneSpecification? zoneSpecification, |
| 18 | + }) { |
| 19 | + final sentryOnError = (exception, stackTrace) async { |
| 20 | + final options = hub.options; |
| 21 | + await _captureError(hub, options, exception, stackTrace); |
| 22 | + |
| 23 | + if (onError != null) { |
| 24 | + onError(exception, stackTrace); |
| 25 | + } |
| 26 | + }; |
| 27 | + |
| 28 | + final userPrint = zoneSpecification?.print; |
| 29 | + |
| 30 | + final sentryZoneSpecification = ZoneSpecification.from( |
| 31 | + zoneSpecification ?? ZoneSpecification(), |
| 32 | + print: (self, parent, zone, line) async { |
| 33 | + final options = hub.options; |
| 34 | + |
| 35 | + if (userPrint != null) { |
| 36 | + userPrint(self, parent, zone, line); |
| 37 | + } |
| 38 | + |
| 39 | + if (!options.enablePrintBreadcrumbs || !hub.isEnabled) { |
| 40 | + // early bail out, in order to better guard against the recursion |
| 41 | + // as described below. |
| 42 | + parent.print(zone, line); |
| 43 | + return; |
| 44 | + } |
| 45 | + if (_isPrinting) { |
| 46 | + // We somehow landed in a recursion. |
| 47 | + // This happens for example if: |
| 48 | + // - hub.addBreadcrumb() called print() itself |
| 49 | + // - This happens for example if hub.isEnabled == false and |
| 50 | + // options.logger == dartLogger |
| 51 | + // |
| 52 | + // Anyway, in order to not cause a stack overflow due to recursion |
| 53 | + // we drop any further print() call while adding a breadcrumb. |
| 54 | + parent.print( |
| 55 | + zone, |
| 56 | + 'Recursion during print() call.' |
| 57 | + 'Abort adding print() call as Breadcrumb.', |
| 58 | + ); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + try { |
| 63 | + _isPrinting = true; |
| 64 | + await hub.addBreadcrumb( |
| 65 | + Breadcrumb.console( |
| 66 | + message: line, |
| 67 | + level: SentryLevel.debug, |
| 68 | + ), |
| 69 | + ); |
| 70 | + parent.print(zone, line); |
| 71 | + } finally { |
| 72 | + _isPrinting = false; |
| 73 | + } |
| 74 | + }, |
| 75 | + ); |
| 76 | + return runZonedGuarded( |
| 77 | + body, |
| 78 | + sentryOnError, |
| 79 | + zoneValues: zoneValues, |
| 80 | + zoneSpecification: sentryZoneSpecification, |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + static Future<void> _captureError( |
| 85 | + Hub hub, |
| 86 | + SentryOptions options, |
| 87 | + Object exception, |
| 88 | + StackTrace stackTrace, |
| 89 | + ) async { |
| 90 | + options.logger( |
| 91 | + SentryLevel.error, |
| 92 | + 'Uncaught zone error', |
| 93 | + logger: 'sentry.runZonedGuarded', |
| 94 | + exception: exception, |
| 95 | + stackTrace: stackTrace, |
| 96 | + ); |
| 97 | + |
| 98 | + // runZonedGuarded doesn't crash the app, but is not handled by the user. |
| 99 | + final mechanism = Mechanism(type: 'runZonedGuarded', handled: false); |
| 100 | + final throwableMechanism = ThrowableMechanism(mechanism, exception); |
| 101 | + |
| 102 | + final event = SentryEvent( |
| 103 | + throwable: throwableMechanism, |
| 104 | + level: options.markAutomaticallyCollectedErrorsAsFatal |
| 105 | + ? SentryLevel.fatal |
| 106 | + : SentryLevel.error, |
| 107 | + timestamp: hub.options.clock(), |
| 108 | + ); |
| 109 | + |
| 110 | + // marks the span status if none to `internal_error` in case there's an |
| 111 | + // unhandled error |
| 112 | + hub.configureScope( |
| 113 | + (scope) => scope.span?.status ??= const SpanStatus.internalError(), |
| 114 | + ); |
| 115 | + |
| 116 | + await hub.captureEvent(event, stackTrace: stackTrace); |
| 117 | + } |
| 118 | +} |
0 commit comments