handleDefault<T> method
Future<Either<Failure, T> >
handleDefault<T>(
- Future<
Either< callback(Failure, T> >- Left<
Failure, T> left(- Failure failure
- Right<
Failure, T> right(- T result
- Left<
- Either<
Failure, T> ? onError(- Object error,
- Left<
Failure, T> left(- Failure failure
- Right<
Failure, T> right(- T response
- StackTrace stackTrace,
inherited
Provides you with an easy way to handle common service logic This helper will help you with some boilerplate needed when implementing handling of async service logic following our architecture
callback
pass your function and perform you required logic, if an exception arise, let the function handle for you or
implement you custom handle with onError
callback
onError
can be provided to handle a specific exception on your repo/service, if you return null, it will be interpreted as not handled
and the function will proceed to handle it itself
Implementation
@override
Future<Either<Failure, T>> handleDefault<T>(
Future<Either<Failure, T>> Function(
Left<Failure, T> Function(Failure failure) left,
Right<Failure, T> Function(T result) right,
)
callback, {
Either<Failure, T>? Function(Object error, Left<Failure, T> Function(Failure failure) left,
Right<Failure, T> Function(T response) right, StackTrace stackTrace)?
onError,
}) async {
failureCbk(Failure failure) {
return Left<Failure, T>(failure);
}
rightCbk(T result) {
return Right<Failure, T>(result);
}
try {
try {
final response = await callback(failureCbk, rightCbk);
return response;
} catch (e, s) {
// check if user have a specific handler for this exception
final Either<Failure, T>? handle = onError?.call(e, failureCbk, rightCbk, s);
if (handle != null) {
return handle;
}
logger.v("Error not handled by user, so handling by default logic");
// if it was not handled then rethrow so we handle it ourselves
rethrow;
}
} on ServerException catch (e, s) {
logger.e("ServerFailure", e, s);
return left<Failure, T>(
ServerFailure.fromServerException(e),
);
} on SocketException catch (e, s) {
logger.e("InternetConnectionIssueFailure", e, s);
return left<Failure, T>(
InternetConnectionIssueFailure.fromSocketException(e),
);
} catch (e, s) {
logger.e("UnknownFailure", e, s);
return left<Failure, T>(UnknownFailure.fromException(e));
}
}