【问题标题】:onError not implemented error in RxJava2, even though its implementedonError 在 RxJava2 中未实现错误,即使它已实现
【发布时间】:2018-03-26 04:13:12
【问题描述】:

所以我有一个非常基本的 RxJava 观察者流工作流程,我在其中请求改造,成功响应我祝酒成功消息和错误我祝酒错误消息。

我下面提到的情况是我期望来自API的错误消息的错误情况,我将其转换为用户可读的单词并显示为Toast,如下所示当我使用doOnNextdoOnError方法时它因提到的错误而崩溃的方式。

我还添加了throwExceptionIfFailure 方法,它显示了我如何转换可读的消息以及控制台指向错误的行。

registerNFCTag(body)
                .map(result -> throwExceptionIfFailure(result))
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(result -> {
                  toggleLoaders(true);                        
                 appToast(getString(R.string.done_msg) + tagName);
                })
                .doOnError(throwable -> {
                    Toasty.error(this, throwable.getLocalizedMessage()).show();
                    toggleLoaders(true);
                })
                .subscribeOn(Schedulers.io())
                .subscribe();

错误如果这还不够,我也可以发布堆栈跟踪。

java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` handling.

ThrowExceptionIfFailure 方法。

public <T> T throwExceptionIfFailure(T res) {

    Response result = (Response<?>) res;
    if (!result.isSuccessful()) {
        try {
            String msg = result.errorBody().string();
            Log.d(TAG, "throwExceptionIfFailure: "+msg);
            if (result.code() == 401 || result.code() == 403) {
                invalidateToken();
                msg = context.getString(R.string.invalid_credential);
            }
            else if (result.code() == 502)
                msg = context.getString(R.string.server_down);
            else if (result.code() == 422)
                msg = context.getString(R.string.invalid_domain);
            else if (result.code() == 500)
                msg = context.getString(R.string.internal_server_error_500_msg);
            else if (result.code() == 451)
------><>>>>>> expected error msg works well with the case mentioned below with throwable in subscribe itself.
                msg = context.getString(R.string.toast_tag_already_registered_error);

            if (result.code() == 403)
                throw new TokenException();
            else
------>>>>>below line where console points error                  
                throw Exceptions.propagate(new RuntimeException(msg)); 

        } catch (Throwable e) {
            throw Exceptions.propagate(e);
        }
    } else {
        return res;
    }
}

但是我以这种方式订阅了同样的东西,它工作正常,我看到错误消息按预期烘烤。

registerNFCTag(body)
                .map(result ->throwExceptionIfFailure(result))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(
                        result -> {
                            toggleLoaders(true);
                            appToast(getString(R.string.done_msg) + tagName);
                        }
                        , throwable -> {
                            Toasty.error(this, throwable.getLocalizedMessage()).show();
                            toggleLoaders(true);
                        });

在 RxJava2 世界中还是新手,请帮助我了解其中的区别。先感谢您。

【问题讨论】:

  • 错误处理需要在subscribe() 作为第二个参数进行。如果它不存在,那么您会收到未处理错误的通知。 doOnError() 实际上并不是一个错误处理程序,尽管它的名字有所暗示。
  • 好吧,那么在第一种情况下,我应该如何处理 doOnError() 中的错误?
  • 为什么您不喜欢您在问题末尾发布的工作示例?您的具体问题是什么?
  • 很好,我只用那个,但我只是好奇想知道。

标签: java android error-handling rx-java2 rx-android


【解决方案1】:

从 RxJava 2.1.9 开始,有 six overloadssubscribe() 方法。你使用了一个重载,它根本不需要消费者。异常消息告诉您,您应该使用 subscribe() 重载,这会导致错误 Consumer,例如subscribe(Consumer&lt;? super T&gt; onNext, Consumer&lt;? super java.lang.Throwable&gt; onError).

doOnError()“副作用”运算符,与实际订阅无关。

【讨论】:

    猜你喜欢
    • 2018-10-04
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多