管理来自不同 rx.Observables 的不同异常 [英] Managing different Exceptions from different rx.Observables

查看:55
本文介绍了管理来自不同 rx.Observables 的不同异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照此处讨论的主题.我正在使用 Clean Architecture 编写一个 Android 应用程序.我有一个交互器负责检索用户的提要数据.流程是这样的:

Following the topic discussed here. I'm coding an Android App using the Clean Architecture. I've an Interactor that takes care of retriving the User's feed data. The flow is like this:

  • 我必须从调用 Retrofit 服务的存储库中获取 Feed 数据以执行 API 调用.
  • 如果出现问题,我必须从与 Sqlite 内部协同工作的 FeedCache 中获取提要数据.
  • 我必须将此提要集合与另一个名为 PendingPostCache 的缓存中的另一组提要合并.此缓存包含用户无法发布的所有文章(因为出现问题、没有互联网连接等)

我的 FeedCache 和 PendingPostCache 都使用 Sqlite.如果出现问题,Botch 可以抛出 DBExceptions.我的 FeedRepository 对服务器端发出请求的那些也可以在出现问题时抛出异常 (ServerSideException).

My FeedCache and PendingPostCache both work with Sqlite. Botch can throw DBExceptions if something went wrong. My FeedRepository the ones that makes the requests against the server-side can also throw exceptions if something goes wrong (ServerSideException).

这是我的交互器的全部代码:

Here's the whole code from my Interactor:

mFeedRepository.getFeed(offset, pageSize) //Get items from the server-side
                .onErrorResumeNext(mFeedCache.getFeed(userSipid)) //If something goes wrong take it from cache
                .mergeWith(mPendingPostCache.getAllPendingPostsAsFeedItems(user)) //Merge the response with the pending posts
                .subscribe(new DefaultSubscriber<List<BaseFeedItem>>() {
                    @Override
                    public void onNext(List<BaseFeedItem> baseFeedItems) {
                        callback.onFeedFetched(baseFeedItems);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ServerSideException) {
                            //Handle the http error
                        } else if (e instanceof DBException) {
                            //Handle the database cache error
                        } else {
                            //Handle generic error
                        }
                    }
                });

我不喜欢那些instanceof.我正在考虑创建一个自定义订阅者,类似于 MyAppSubscriber,它实现了 onError 方法,进行这些实例比较,并执行一些称为 onServerSideError()、onDBError() 的方法.这样,代码会变得更清晰,我可以省去编写样板代码的实例.有人对如何解决这个问题有更好的想法吗?一些避免自定义订阅者的方法?

I don't like having those instanceof. I'm thinking on creating a custom subscriber, something called like MyAppSubscriber, which implements the onError method, makes those instanceof comparations, and execute some methods called onServerSideError(), onDBError(). That way the code is going te be a lot cleaner and I can spare writing that instanceof boilerplate code. Has someone a better idea about how to approach this issue? Some way to avoid the custom Subscriber?

推荐答案

只需使用组合:

public <T,E> Function<Throwable, Observable<T>> whenExceptionIs(Class<E> what, Function<E, Observable<T>> handler) {
  return t -> {
    return what.isInstance(t) ? handler.apply(what.cast(t)) : Observable.error(t);
  };
}

那你就正常使用吧:

Observable.from(...).flatMap(...)
.onErrorResumeNext(whenExceptionIs(ServerSideException.class, e-> Observable.empty()))
.onErrorResumeNext(whenExceptionIs(DBException.class, e-> ...))

你甚至可以用一种方法抽象所有这些:

You can even abstract all that in one method:

public <T> Transformer<T, T> errorHandling() {
   return src -> src
      .onErrorResumeNext(whenExceptionIs(ServerSideException.class, e-> Observable.empty()))
      .onErrorResumeNext(whenExceptionIs(DBException.class, e-> ...));
}


Observable.from(...).flatMap(...)
.compose(errorHandling())
.subscribe();

这篇关于管理来自不同 rx.Observables 的不同异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆