io.reactivex.exceptions.UndeliverableException The exception could not be delivered to the consumer because it has already canceled/disposed
Asked Answered
A

3

15

Getting an UndeliverableException while using completable

public Completable createBucketWithStorageClassAndLocation() {
        return Completable.complete()
                .doFinally(() -> {
            Bucket bucket =
                    storage.create(
                            BucketInfo.newBuilder(googleUploadObjectConfiguration.bucketName())
                                    .setStorageClass(storageClass)
                                    .setLocation(googleUploadObjectConfiguration.locationName())
                                    .build());       
        }).doOnError(error -> LOG.error(error.getMessage()));
    }

The exception is thrown from the Google storage which is correct, But trying to handle on doOnError method

Caused by: com.google.cloud.storage.StorageException: You already own this bucket. Please select another name.

RXJava exception

io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | com.google.cloud.storage.StorageException: You already own this bucket. Please select another name.
    at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367)
    at io.reactivex.internal.operators.completable.CompletableDoFinally$DoFinallyObserver.runFinally(CompletableDoFinally.java:99)
    at io.reactivex.internal.operators.completable.CompletableDoFinally$DoFinallyObserver.onComplete(CompletableDoFinally.java:79)
    at io.micronaut.reactive.rxjava2.RxInstrumentedCompletableObserver.onComplete(RxInstrumentedCompletableObserver.java:64)
    at io.reactivex.internal.disposables.EmptyDisposable.complete(EmptyDisposable.java:68)
    at io.reactivex.internal.operators.completable.CompletableEmpty.subscribeActual(CompletableEmpty.java:27)
    at io.reactivex.Completable.subscribe(Completable.java:2309)
    at io.micronaut.reactive.rxjava2.RxInstrumentedCompletable.subscribeActual(RxInstrumentedCompletable.java:51)
    at io.reactivex.Completable.subscribe(Completable.java:2309)
    at io.reactivex.internal.operators.completable.CompletableDoFinally.subscribeActual(CompletableDoFinally.java:43)
    at io.reactivex.Completable.subscribe(Completable.java:2309)
    at io.micronaut.reactive.rxjava2.RxInstrumentedCompletable.subscribeActual(RxInstrumentedCompletable.java:51)
    at io.reactivex.Completable.subscribe(Completable.java:2309)
    at io.reactivex.internal.operators.completable.CompletablePeek.subscribeActual(CompletablePeek.java:51)
    at io.reactivex.Completable.subscribe(Completable.java:2309)
    at io.micronaut.reactive.rxjava2.RxInstrumentedCompletable.subscribeActual(RxInstrumentedCompletable.java:51)
    at io.reactivex.Completable.subscribe(Completable.java:2309)
    at io.reactivex.Completable.subscribe(Completable.java:2410)
    at fete.bird.StartUp.onApplicationEvent(StartUp.java:24)
    at fete.bird.StartUp.onApplicationEvent(StartUp.java:12)
    at io.micronaut.context.DefaultBeanContext.notifyEventListeners(DefaultBeanContext.java:1323)
    at io.micronaut.context.DefaultBeanContext.publishEvent(DefaultBeanContext.java:1308)
    at io.micronaut.http.server.netty.NettyHttpServer.fireStartupEvents(NettyHttpServer.java:507)
    at io.micronaut.http.server.netty.NettyHttpServer.start(NettyHttpServer.java:350)
    at io.micronaut.http.server.netty.NettyHttpServer.start(NettyHttpServer.java:113)
    at io.micronaut.runtime.Micronaut.lambda$start$2(Micronaut.java:77)
    at java.base/java.util.Optional.ifPresent(Optional.java:176)
    at io.micronaut.runtime.Micronaut.start(Micronaut.java:75)
    at io.micronaut.runtime.Micronaut.run(Micronaut.java:311)
    at io.micronaut.runtime.Micronaut.run(Micronaut.java:297)
    at fete.bird.FeteBirdServiceApplication.main(FeteBirdServiceApplication.java:16)

From the rxjava documentation https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling I need to handle the error in the application.

I need to write the below code,

/ If Java 8 lambdas are supported
RxJavaPlugins.setErrorHandler(e -> { });

My question is where should I write this code. I have a Micronaut application using java or this is the only way to handle the exception.

Archive answered 11/3, 2021 at 5:1 Comment(0)
T
2

Use Completable.fromAction and perhaps try-catch the exception instead of that doFinally contraption:

Completable.fromAction(() -> {
    try {
        Bucket bucket = storage.create(
            BucketInfo.newBuilder(googleUploadObjectConfiguration.bucketName())
                      .setStorageClass(storageClass)
                      .setLocation(googleUploadObjectConfiguration.locationName())
                      .build()); 
    } catch (Throwable error) {
        LOG.error(error.getMessage());
    }
})
Taciturnity answered 11/3, 2021 at 10:17 Comment(2)
Is there any other way to catch the exception instead of try and catch ?Archive
You could use onErrorResumeNext() to convert the failure to some other Completable. github.com/ReactiveX/RxJava/wiki/Error-Handling-OperatorsTaciturnity
M
15

You should add this in you application class

RxJavaPlugins.setErrorHandler(e -> { });

RxJavaPlugins.setErrorHandler { e ->
if (e is UndeliverableException) {
    // Merely log undeliverable exceptions
    log.error(e.message)
} else {
    // Forward all others to current thread's uncaught exception handler
    Thread.currentThread().also { thread ->
        thread.uncaughtExceptionHandler.uncaughtException(thread, e)
    }
}

More info here https://github.com/instacart/truetime-android/issues/98

Mauritamauritania answered 22/6, 2021 at 10:16 Comment(0)
T
2

Use Completable.fromAction and perhaps try-catch the exception instead of that doFinally contraption:

Completable.fromAction(() -> {
    try {
        Bucket bucket = storage.create(
            BucketInfo.newBuilder(googleUploadObjectConfiguration.bucketName())
                      .setStorageClass(storageClass)
                      .setLocation(googleUploadObjectConfiguration.locationName())
                      .build()); 
    } catch (Throwable error) {
        LOG.error(error.getMessage());
    }
})
Taciturnity answered 11/3, 2021 at 10:17 Comment(2)
Is there any other way to catch the exception instead of try and catch ?Archive
You could use onErrorResumeNext() to convert the failure to some other Completable. github.com/ReactiveX/RxJava/wiki/Error-Handling-OperatorsTaciturnity
D
1

Like @jonathan said, to solve this, I simply add in my App class the following code :

RxJavaPlugins.setErrorHandler { error ->
  var e = error
  if (e is UndeliverableException) {
      e = e.cause ?:let { e }
  }
  if (e is IOException || e is SocketException) {
      // fine, irrelevant network problem or API that throws on cancellation
      return@setErrorHandler
  }
  if (e is InterruptedException) {
      // fine, some blocking code was interrupted by a dispose call
      return@setErrorHandler
  }
  if (e is NullPointerException || e is IllegalArgumentException) {
    // that's likely a bug in the application
    Thread.currentThread().uncaughtExceptionHandler?.uncaughtException(Thread.currentThread(), e)?:let {
        FirebaseCrashlytics.log(Log.ERROR, TAG, "RxJavaPlugins uncaughtExceptionHandler is null but error is NullPointerException || error is IllegalArgumentException : $e")
    }
    return@setErrorHandler
  }
  if (e is IllegalStateException) {
    // that's a bug in RxJava or in a custom operator
    Thread.currentThread().uncaughtExceptionHandler?.uncaughtException(Thread.currentThread(), e)?:let {
        FirebaseCrashlytics.log(Log.ERROR, TAG, "RxJavaPlugins uncaughtExceptionHandler is null but error is IllegalStateException : $e")
    }
    return@setErrorHandler
  }
  FirebaseCrashlytics.log(Log.ERROR, TAG, "Undeliverable exception received, not sure what to do $e")
}

You can adapt this code with your own Log system if you don't use FirebaseCrashlytics. This code is edited from the source code in https://github.com/ReactiveX/RxJava/wiki/What%27s-different-in-2.0#error-handling (URL indicate in the error itself).

Ditto answered 3/6, 2022 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.