Life is a fucking movie. 😈 😈 😈 😈 😈 😈
人生如戏。
🗑 Error Handle Of Rxjava
License: Apache License 2.0
使用errorHandler后subscribe 返回void 怎么返回Disposable 我想要管理Disposable
因为大部分页面都需要加上错误的缺省页,能不能在handleError里传入一个自动view方便做全局处理?
https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling
RxJavaPlugins.setErrorHandler(e -> {
if (e instanceof UndeliverableException) {
e = e.getCause();
}
if ((e instanceof IOException) || (e instanceof SocketException)) {
// fine, irrelevant network problem or API that throws on cancellation
return;
}
if (e instanceof InterruptedException) {
// fine, some blocking code was interrupted by a dispose call
return;
}
if ((e instanceof NullPointerException) || (e instanceof IllegalArgumentException)) {
// that's likely a bug in the application
Thread.currentThread()
.getUncaughtExceptionHandler()
.uncaughtException(Thread.currentThread(), e);
return;
}
if (e instanceof IllegalStateException) {
// that's a bug in RxJava or in a custom operator
Thread.currentThread()
.getUncaughtExceptionHandler()
.uncaughtException(Thread.currentThread(), e);
return;
}
});
您好,我fork下了您的 RxErrorHandler, 有幸拜读了源码,我很喜欢这个库,它将 网络请求过程中的异常 统一交给了 ErrorHandlerFactory 去处理。
但是我遇到了两个问题:
我比较喜欢使用lambda,因此直接new ErrorHandleSubscriber可能会让我有点难受。
重写ErrorHandleSubscriber 的onError是一个不错的主意,但是如果 针对固定的网络情况做统一的处理, 比如连接超时统一弹出ErrorDialog,我也会觉得有点麻烦。
灵感来源于 AutoDispose ,我设想了这样一种方式,通过其他的Rx操作符,将RxJava的数据类型进行一次包装,这样无论是Obervable,还是Flowable,通过as()操作符统一生成对应的Wraper类:
public interface ErrorHandlerConverter<T> extends FlowableConverter<T, FlowableSubscribeProxy<T>>,
ObservableConverter<T, ObservableSubscribeProxy<T>>,
MaybeConverter<T, MaybeSubscribeProxy<T>>,
SingleConverter<T, SingleSubscribeProxy<T>>,
CompletableConverter<CompletableSubscribeProxy> {
}
通过实现不同 响应式类型 的包装类,以Observer为例:
final class ErrorHandlerObserverImpl<T> extends AtomicInteger implements AutoDisposingObserver<T> {
//...
@Override public void onError(Throwable e) {
mHandlerFactory.handleError(t);
}
}
在实际生产代码中,只需要这样实现:
// APP初始化
mRxErrorHandler = RxErrorHandler
.builder()
.responseErrorListener(new ResponseErrorListener() {
@Override
public void handleResponseError(Activity activity, Throwable t) {
if (t instanceof UnknownHostException) {
//do something ...
} else if (t instanceof SocketTimeoutException) {
//do something ...
} else if (t instanceof ParseException || t instanceof JSONException) {
//do something ...
} else {
//handle other Exception ...
}
Log.w(TAG, "Error handle");
}
}).build();
// Activity中
requestUserInfo(userName)
.as(RxErrorHandler.handleError(MainActivity.this)) //通过ErrorHandlerConverter函数,根据不同的响应式类型返回不同的Wraper类
.subscribe(info -> mRootView.onGetUserInfo(info),
e -> {
//如有特殊需求可以额外实现onError(),as操作产生的WrapObservable已经进行了全局处理
mRootView.onError("请求出现异常");
});
以上纯属空想...而且这种方式感觉是,虽然方便了开发者,但是对于库来讲,是一个巨大的工程,不知道这种设计是否可行,且是否有足够的必要,希望能够得到您宝贵的建议 😄 !
There's a typo in the description (and it doesnt match the proper class name anyway).
ResponseErroListener must be ResponseErrorListener (missing r).
Regards, Emanuel
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.