execute<T> method

Future<T> execute<T>(
  1. Future<T> action()
)

Executes action through the circuit breaker.

Throws AINetworkError if the circuit is open.

Implementation

Future<T> execute<T>(Future<T> Function() action) async {
  if (!isAllowed) {
    throw AINetworkError(
      provider: name,
      message:
          'Circuit breaker is open — provider "$name" is temporarily unavailable. '
          'Will retry after ${resetTimeout.inSeconds}s.',
    );
  }

  try {
    final result = await action();
    recordSuccess();
    return result;
  } catch (e) {
    recordFailure();
    rethrow;
  }
}