Description
In the project error handling is show error message in presentation layer.
But if we need add exception NotAuthorizedException in data layer and handle all error from RestApi with 401 code error. In presentation layer we need handle this error and show AuthActivity.
@Override
public void onError(Throwable e) {
UserDetailsPresenter.this.hideViewLoading();
if (e instanceof NotAuthorizedException) {
UserDetailsPresenter.this.showAuthScreen();
} else {
UserDetailsPresenter.this.showErrorMessage(new DefaultErrorBundle((Exception) e));
}
UserDetailsPresenter.this.showViewRetry();
}
Can we have a global error handler for each presenter (preferably for the entire application at once) for certain types of errors? Thus we do not have to duplicate code in each subscriber, furthermore, we can have a default subscriber.
Perhaps this approach is a bit contrary to RxJava philosophy, but in this case it seems to me it is justified.
Any ideas on this?
Domain
public interface RestApiErrorHandling {
void handle (Throwable throwable);
}
public abstract class UseCase {
private RestApiErrorHandling restApiErrorHandling;
publlic void registerErrorHandling(RestApiErrorHandling restApiErrorHandling) {
this.restApiErrorHandling = restApiErrorHandling;
}
protected abstract Observable buildUseCaseObservable();
public void execute(Subscriber UseCaseSubscriber) {
this.subscription = this.buildUseCaseObservable()
.doOnError(throwable -> {
if (restApiErrorHandling != null) {
restApiErrorHandling.handle(throwable);
}
})
.subscribeOn(Schedulers.from(threadExecutor))
.observeOn(postExecutionThread.getScheduler())
.subscribe(UseCaseSubscriber);
}
}
Presentation
public class UserDetailsPresenter implements RestApiErrorHandling {
private final UseCase getUserDetailsUseCase;
private final UseCase shareUserUseCase;
private final UseCase addToFavoriteUserUseCase;
public class UserDetailsPresenter(@Named("GetUserDetailsUseCase") UseCase getUserDetailsUseCase,
@Named("ShareUserUseCase") UseCase shareUserUseCase,
@Named("AddToFavoriteUserUseCase") UseCase addToFavoriteUserUseCase) {
this.getUserDetailsUseCase = getUserDetailsUseCase;
this.shareUserUseCase = shareUserUseCase;
this.addToFavoriteUserUseCase = addToFavoriteUserUseCase;
getUserDetailsUseCase.registerErrorHandling(this);
shareUserUseCase.registerErrorHandling(this);
addToFavoriteUserUseCase.registerErrorHandling(this);
}
private void getUserDetails() {
this.getUserDetailsUseCase.execute(new UserDetailsSubscriber());
}
public void shareUser() {
this.shareUserUseCase.execute(new DefaultSubscriber());
}
public void addToFavoriteUser() {
this.addToFavoriteUserUseCase.execute(new DefaultSubscriber());
}
@Override
public void handle (Throwable throwable) {
if (throwable instanceof NotAuthorizedException){
this.viewDetailsView.showAuthScreen();
}
}
}
P.S. What about the EventBus?
Error handler in the Data layer can sent error via the EventBus error to the Presentation layer?
For example, if an application is built on the one Activity, it is possible to transmit an error on the main presenter and show auth screen.
It will be correct?