guard<T> static method

Future<SintStatus<T>> guard<T>(
  1. Future<T> task(), {
  2. T? previousData,
  3. bool isEmpty(
    1. T data
    )?,
})

Executes task and captures its outcome into a SintStatus without throwing (SuccessStatus on completion, ErrorStatus on exception).

Implementation

static Future<SintStatus<T>> guard<T>(
  Future<T> Function() task, {
  T? previousData,
  bool Function(T data)? isEmpty,
}) async {
  try {
    final result = await task();
    if (isEmpty != null && isEmpty(result)) {
      return SintStatus<T>.empty();
    }
    return SintStatus<T>.success(result);
  } catch (error, stackTrace) {
    return SintStatus<T>.error(
      error,
      previousData: previousData,
      stackTrace: stackTrace,
    );
  }
}