RXJava2 + AWS SDK:从AWS S3 TransferListener调用onError时获取UndeliverableException

时间:2017-06-02 14:15:30

标签: amazon-web-services amazon-s3 rx-android rx-java2

我正在使用RXJava(RXAndroid)来序列化一系列AWS s3传输请求。

我正在使用AWS TransferUtility,它提供了一个获取进度和错误通知的异步TransferListener。

我很难传播这些错误。

我的代码看起来像这样

    for (final String subscription : subscriptions) {

        Observable<String> thisObservable = new Observable<String>() {
            @Override
            protected void subscribeActual(Observer<? super String> observer) {
                new S3Loader(subscription, observer).load();
                // The S3Loader uses AWS TransferListener to call onNext and onError of the observers
            }
        };

        observables.add(thisObservable);

        Log.v(this, "Added %s", subscription);

    }

    Observable.concat(observables)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<String>() {

                     @Override
                     public void onSubscribe(@NonNull Disposable d) {
                         Log.v(OperationalStatusFragment.this, "Subscribed...");
                     }

                     @Override
                     public void onNext(@NonNull String s) {
                         Log.v(OperationalStatusFragment.this, "Started weather operation \"%s\"", s);
                     }

                     @Override
                     public void onError(@NonNull Throwable e) {
                         Log.e(OperationalStatusFragment.this, "An error happened...");
                     }

                     @Override
                     public void onComplete() {
                         Log.v(OperationalStatusFragment.this, "Completed...");

                     }
                 }
    );

基本上,当TransferListebner中的代码调用onError时,将调用观察者onError块,但是应用程序在此之后崩溃,因为我得到了抛出但是包含在UndeliverableException中的相同异常。

感觉这与transferListener的异步性质有关(就像我同步调用相同的onError方法一样,它按预期工作。

知道出了什么问题吗?

编辑1:这是堆栈跟踪的样子

06-02 18:49:23.286 11687-11687/com.ironbird.fav2 E/AndroidRuntime: FATAL EXCEPTION: main
       Process: com.ironbird.fav2, PID: 11687
       io.reactivex.exceptions.UndeliverableException: java.io.IOException: Got failed while downloading weather DB for "FRxxx" from "weather/06/weather_FRxxx.db" to "/data/user/0/com.ironbird.fav2/app_weather/FRxxx.db" (transfer #568)
           at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:349)
           at io.reactivex.internal.operators.observable.ObservableConcatMap$ConcatMapDelayErrorObserver$DelayErrorInnerObserver.onError(ObservableConcatMap.java:509)
           at com.ironbird.fav2.weather.S3loader$1.onStateChanged(S3loader.java:79)
           at com.amazonaws.mobileconnectors.s3.transferutility.TransferStatusUpdater$1.run(TransferStatusUpdater.java:172)
           at android.os.Handler.handleCallback(Handler.java:751)
           at android.os.Handler.dispatchMessage(Handler.java:95)
           at android.os.Looper.loop(Looper.java:154)
           at android.app.ActivityThread.main(ActivityThread.java:6119)
           at java.lang.reflect.Method.invoke(Native Method)
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
        Caused by: java.io.IOException: Got failed while downloading weather DB for "FRxxx" from "weather/06/weather_FRxxx.db" to "/data/user/0/com.ironbird.fav2/app_weather/FRxxx.db" (transfer #568)
           at com.ironbird.fav2.weather.S3loader$1.onStateChanged(S3loader.java:79) 
           at com.amazonaws.mobileconnectors.s3.transferutility.TransferStatusUpdater$1.run(TransferStatusUpdater.java:172) 
           at android.os.Handler.handleCallback(Handler.java:751) 
           at android.os.Handler.dispatchMessage(Handler.java:95) 
           at android.os.Looper.loop(Looper.java:154) 
           at android.app.ActivityThread.main(ActivityThread.java:6119) 
           at java.lang.reflect.Method.invoke(Native Method) 
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

编辑2:这是S3loader.load()的样子:

public void load() {

    observer.onNext(subscription);

        AmazonS3Client s3Client = new AmazonS3Client();
        TransferUtility transferUtility = new TransferUtility(s3Client, context);
        TransferObserver observer = transferUtility.download(BUCKET, subscription, file);
        observer.setTransferListener(new TransferListener() {

            @Override
            public void onStateChanged(int id, TransferState state) {

                if (state == TransferState.COMPLETED) {
                    observer.onComplete();
                } else if (state == TransferState.FAILED) {
                    observer.onError(new IOException("An error happened"));
                } 
            }

            @Override
            public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {

            }

            @Override
            public void onError(int id, Exception e) {

                observer.onError(e);
            }
        });

}

0 个答案:

没有答案