install method

  1. @override
void install()
override

Wire the watcher's listening hook (event subscription, override, etc.).

Implementation

@override
void install() {
  if (_installed) return;
  _installed = true;

  // 1. Chain-preserve and replace FlutterError.onError (sync framework errors).
  _previousFlutterOnError = FlutterError.onError;
  FlutterError.onError = (FlutterErrorDetails details) {
    TelescopeStore.recordException(
      ExceptionRecord(
        exceptionType: details.exception.runtimeType.toString(),
        message: details.exceptionAsString(),
        time: DateTime.now(),
        stackTrace: details.stack?.toString(),
      ),
    );
    _previousFlutterOnError?.call(details);
  };

  // 2. Chain-preserve and replace PlatformDispatcher.onError (async + isolate + plugin errors).
  _previousPlatformOnError = PlatformDispatcher.instance.onError;
  PlatformDispatcher.instance.onError = (Object error, StackTrace stack) {
    TelescopeStore.recordException(
      ExceptionRecord(
        exceptionType: error.runtimeType.toString(),
        message: error.toString(),
        time: DateTime.now(),
        stackTrace: stack.toString(),
      ),
    );
    // Chain-preserve the previous handler's return value so downstream
    // observability tools (Sentry, Bugsnag) keep their fatal-propagation
    // semantics. If no previous handler exists, default to `true` (handled)
    // to match the legacy single-hook behavior.
    return _previousPlatformOnError?.call(error, stack) ?? true;
  };
}