safeCall method

FutureOr<Either<RepositoryError, ValueType>> safeCall({
  1. required FutureOr<Either<RepositoryError, ValueType>> call(),
})

Executes a function safely, catching defined exceptions and mapping them to Left.

Use this wrapper to ensure your repository never throws an exception to the domain layer.

Flow:

  1. Executes call.
  2. If successful, returns Right (or whatever call returns).
  3. If UnControlDataSourceException is thrown -> returns Left via onUnControlException.
  4. If InadmissibleDataSourceException is thrown -> returns Left via onInadmissibleException.
  5. If any other Exception is thrown -> returns Left via onException.

Implementation

FutureOr<Either<RepositoryError, ValueType>> safeCall({
  required FutureOr<Either<RepositoryError, ValueType>> Function() call,
}) async {
  try {
    return await call();
  } on UnControlDataSourceException catch (e, s) {
    return Left(onUnControlException(e, s));
  } on InadmissibleDataSourceException catch (e, s) {
    return Left(onInadmissibleException(e, s));
  } catch (exception, stackTrace) {
    // Catch-all for unexpected errors (parsing, null pointers, etc.)
    return Left(onException(exception, stackTrace));
  }
}