android10 / android10/Android-CleanArchitecture

What about global error handling?

Đang mở
#170 3 bình luận 6 reaction 0 người được giao Xem trên GitHub
discussion question
Ngôn ngữ chính
Java
Star
15.5k
Fork
3.3k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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?

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.