handleYamuxOperation<T> static method

Future<T> handleYamuxOperation<T>(
  1. Future<T> operation(), {
  2. int? streamId,
  3. String? operationName,
  4. String? currentState,
  5. Map<String, dynamic>? context,
})

Safely executes a Yamux operation with comprehensive exception handling

Implementation

static Future<T> handleYamuxOperation<T>(
  Future<T> Function() operation, {
  int? streamId,
  String? operationName,
  String? currentState,
  Map<String, dynamic>? context,
}) async {
  try {
    return await operation();
  } catch (e, stackTrace) {
    if (e is YamuxException) {
      // Already classified, just rethrow
      rethrow;
    }

    // Handle both Exception and Error types (StateError extends Error, not Exception)
    if (e is Exception || e is Error) {
      final classified = classifyYamuxException(
        e, // Pass the original exception/error directly
        stackTrace,
        streamId: streamId,
        operation: operationName,
        currentState: currentState,
        context: context,
      );

      _log.warning(
        'Yamux operation failed: ${classified.message}',
        classified.originalException,
        classified.originalStackTrace,
      );

      throw classified;
    }

    // Other types (shouldn't happen in normal operation)
    rethrow;
  }
}