0

I have this Android Kotlin code with a list of completables that are merged into a bigger one with mergeDelayError(), that has a timeout. When the timeout is reached, I get java.util.concurrent.TimeoutException: The source did not signal an event for 250 milliseconds and has been terminated.

How can I tell which one of the small completables on the list reached the timeout without signaling/emitting anything (the actual list has more than two BTW). My ideal solution would replace such exception with something like: Exception: The following operations reached a timeout: Check initial settings, Check server, or at least an specific log line printed out by each timed-out completable.

I have no idea how to do this. My first guess would be simply adding a "didEmit" flag to each completable and check them one by one when the timeout is reached on the big completable, but it seems like a bad solution to me.

Any ideas appreciated. Thank you.

        val smallCompletable1 = controller
        .checkInitialSettings()
        .doOnError {
            logError(it)
        }
        .ignoreElement()

        val smallCompletable2 = controller
        .checkServerStatus()
        .doOnError {
            logError(it)
        }
        .ignoreElement()

        val myCompletables: List<Completable> = listOf(
            smallCompletable1,
            smallCompletable2
        )

        val bigDisposable = Completable
        .mergeDelayError(myCompletables)
        .timeout(250, TimeUnit.MILLISECONDS)
        .subscribe(this::handleAllSucceeded, this::handleError)

        composition.add(bigDisposable)
DuckN'Bear
  • 365
  • 1
  • 4
  • 19

1 Answers1

0

Just an idea, but try using a wrapper that would throw your custom defined exception with overrided exception message.

val smallCompletable1 = controller
    .checkInitialSettings()
    .doOnError{...}
    .timeout(250, TimeUnit.MILLISECONDS)
    .ignoreElement()

val completable1 = Completable.create { emitter ->
    smallCompletable1.subscribe(
        { emitter.onComplete() },
        { exp -> emitter.onError([YourCustomException]) }
    )
}
// do the same for smallCompletable2

val myCompletables: List<Completable> = listOf(
    completable1,
    completable2
)
...

Also, I maybe move the timeout to each of the small completables instead. If declaring the timeout at the merged completable, I am not sure if you would get the error(s) from small completables instead of the generic timeout error that is thrown for the merged completable.

Dat Pham Tat
  • 1,433
  • 7
  • 9