I have the following code
class GetAuthUserUseCase {
final AuthRepository _authRepository;
GetAuthUserUseCase({
required AuthRepository authRepository,
}) : _authRepository = authRepository;
Future<Either<Failure, AuthUser>> call() async {
final userResponse = await _authRepository.getUser();
var user = userResponse.fold(
(Failure l) => //How can I return the failure from the call function?,
(AuthUser r) => r,
);
final accessTokenExpiresAtMilliseconds =
user.accessTokenIssuedAtMilliseconds + user.accessTokenExpiresInMilliseconds;
final accessTokenExpiresAtDateTime =
DateTime.fromMillisecondsSinceEpoch(accessTokenExpiresAtMilliseconds);
if (DateTime.now().isBefore(accessTokenExpiresAtDateTime)) {
return userResponse;
}
return _authRepository.refreshUser(
user: user,
);
}
}
In my use case I have some business logic to determines whether I need to refresh a users access token or not. Based on that I refresh my user. But I need to load my user first with getUser()
in order to determine if the access token has expired.
The getUser()
will return an Either<Failure,AuthUser>
so I fold that to get the user. But what I want is tho propagate the failure so it terminates the call method and outputs that failure from the call method.
I would like to avoid doing a type check for Failure or AuthUser if possible since I think it will make my program less robust.
Is this possible?
For this case, you can use
isLeft()
to check the failure. andisRight()
on success.Inside this, you may like to access data directly, this extension will help