safeCall<T> method

  1. @override
Future<Result<T>> safeCall<T>(
  1. Future<T> request(), {
  2. Result<T>? onEndpointError(
    1. DioException
    )?,
})
override

Executes request and maps any thrown error to an Err via the registry. See SimpleNetworkHandler.safeCall for the historical docs; semantics are identical.

Implementation

@override
Future<Result<T>> safeCall<T>(
  Future<T> Function() request, {
  Result<T>? Function(DioException)? onEndpointError,
}) async {
  callCount++;
  final next = _queue.isNotEmpty ? _queue.removeAt(0) : defaultResult;
  if (next == null) {
    throw StateError(
      'FakeNetworkHandler: no queued result for call #$callCount. '
      'Call enqueueOk/enqueueErr before exercising the code under test, '
      'or set defaultResult.',
    );
  }
  return next.fold(
    (failure) => Err<T>(failure),
    (value) {
      if (value is! T) {
        throw StateError(
          'FakeNetworkHandler: call #$callCount expects a $T, but the '
          'queued success carries ${value == null ? 'null' : '${value.runtimeType} ($value)'}. '
          'Queue the type the code under test asks for.',
        );
      }
      return Ok<T>(value);
    },
  );
}