Handle multiple scenario from API call
chrizzz35 opened this issue · comments
Hi, I'm currently following this architecture for my apps, and wondering how to handle multiple scenario code response from API call.
Let's say I have something like this:
UserRepository contain the logic between API fetch or loading from SQLite cache, I can assure that UserLocalDataStore will return the Success Response. The problem is, my UserRemoteData has many scenario, ex:
- Success Response
- Expired Session
- Invalid Token
Currently the UseCase, Repository, LocalDataStore & RemoteDataStore return Observable<UserResponse>
, and I put my validation in my presenter, but feels so wrong because I need to validate it everytime I use GetUserListUseCase.
if(response.getCode().equals(API_SUCCESS)) {
view.showUserList(response.getUserList());
}
else if(response.getCode().equals(EXPIRED_SESSION)) {
view.moveToLoginScreen(response.getMessage());
}
else if(response.getCode().equals(TOKEN_INVALID)) {
view.moveToLoginScreen(response.getMessage());
}
else {
view.showFailedFetchUserList("Unexpected Error Occured");
}
*Note: all of my API fetch can return this response scenario
My Question is:
- Which layer is reponsible for validate this scenario?
- Do I really need to validate this scenario in other API Call for different Entity (Let say I have Product, Purchase entity, instead only User entity. Since all of my API have this scenario)?
Personally, I would encapsulate the logic of expired session and invalid token in the remote data store and every data store would emit users. If error happened then i would pass specific exception (let's call it SessionException) in observable's onError method.
Then I would have an observer inside the UserListPresenter which would have an implementation like that:
private final class UserObserver extends DisposableObserver {
@Override
public void onNext(User module) {
//...
}
@Override
public void onError(Throwable e) {
if (e instanceof SessionException) {
view.moveToLoginScreen(e.getMessage());
} else {
view.showFailedFetchUserList("Unexpected Error Occured");
}
}
@Override
public void onComplete() {
//...
}
}