safeCall method
FutureOr<Either<RepositoryError, ValueType> >
safeCall({
- required FutureOr<
Either< call(),RepositoryError, ValueType> >
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:
- Executes
call. - If successful, returns Right (or whatever
callreturns). - If UnControlDataSourceException is thrown -> returns Left via onUnControlException.
- If InadmissibleDataSourceException is thrown -> returns Left via onInadmissibleException.
- 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));
}
}