Coder Social home page Coder Social logo

rxerrorhandler's Introduction

Hi

Life is a fucking movie. 😈 😈 😈 😈 😈 😈

人生如戏。

rxerrorhandler's People

Contributors

jessyancoding avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

rxerrorhandler's Issues

handleError传入自定义view

因为大部分页面都需要加上错误的缺省页,能不能在handleError里传入一个自动view方便做全局处理?

可以考虑增加对RxJavaPlugins.setErrorHandler的处理

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;
    }
});

关于RxErrorHandler的另外一种实现设想

@JessYanCoding

您好,我fork下了您的 RxErrorHandler, 有幸拜读了源码,我很喜欢这个库,它将 网络请求过程中的异常 统一交给了 ErrorHandlerFactory 去处理。

但是我遇到了两个问题:

  • 1.在订阅时,我需要实现 ErrorHandleSubscriber 的Observer的包装类去处理,且项目中的每一次订阅都需要实现 ErrorHandleSubscriber,并且将rxErrorHandler作为依赖赋予它。

我比较喜欢使用lambda,因此直接new ErrorHandleSubscriber可能会让我有点难受。

  • 2.在初始化创建RxErrorHandler时,通过set方法,放入了一个Application对象,在之后的回调里,如果想直接弹出一个Dialog,对于Application对象来讲会很麻烦。

重写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("请求出现异常");
});

以上纯属空想...而且这种方式感觉是,虽然方便了开发者,但是对于库来讲,是一个巨大的工程,不知道这种设计是否可行,且是否有足够的必要,希望能够得到您宝贵的建议 😄 !

Typo in Description

There's a typo in the description (and it doesnt match the proper class name anyway).

ResponseErroListener must be ResponseErrorListener (missing r).

Regards, Emanuel

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.