android10 / android10/Android-CleanArchitecture
What about global error handling?
- Lingua principale
- Java
- Stelle
- 15.5k
- Fork
- 3.3k
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
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?
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Direzione di ricerca
Inizia dal flusso di UseCase.execute e dagli esempi di gestione degli errori di UserDetailsPresenter mostrati nell’issue; traccia il percorso con cui gli errori di RestApi raggiungono gli iscritti e la presentazione. Poiché non sono stati indicati file del repository o test, individua i punti di ingresso rilevanti per i dati e la presentazione, quindi definisci un criterio concreto di completamento per qualsiasi modifica alla gestione globale.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- android, java
- Ambito
- mobile-dev
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 20/100