0

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?

anonymous-dev
  • 2,897
  • 9
  • 48
  • 112
  • Please use fpdart instead of dartz. It's very likely you'll be much happier with the API, plus it's under significant development and completely documented including many tutorials. – Randal Schwartz Jan 04 '23 at 20:32

1 Answers1

1

For this case, you can use isLeft() to check the failure. and isRight() on success.

Future<Either<Failure, AuthUser>> call() async {
    final userResponse = await _authRepository.getUser();

    if(userResponse.isLeft()){
       // do your stuffs       
     }

Inside this, you may like to access data directly, this extension will help

extension EitherX<L, R> on Either<L, R> {
  R asRight() => (this as Right).value; //
  L asLeft() => (this as Left).value;
}
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56