15

Is there an option to have variant of timeout that does not emit Throwable?

I would like to have complete event emited.

4 Answers 4

23

You don't need to map errors with onErrorResumeNext. You can just provide a backup observable using:

timeout(long,TimeUnit,Observable)

It would be something like:

    .timeout(500, TimeUnit.MILLISECONDS, Observable.empty())
Sign up to request clarification or add additional context in comments.

1 Comment

This should be the accepted answer because the original post is asking for a variant of 'timeout', which this answer provides.
15

You can resume from an error with another Observable, for example :

 Observable<String> data = ...
 data.timeout(1, TimeUnit.SECONDS)
     .onErrorResumeNext(Observable.empty())
     .subscribe(...);

3 Comments

That's what this code do. It will transform your error to a Observable that will complete imediately
It's worth noting that this solution doesn't work if there's a possibility of errors coming from further upstream, because it'll catch those too...
downvote - this will cause all other valid errors to return empty
11

A simpler solution that does not use Observable.timeout (thus it does not generate an error with the risk of catching unwanted exceptions) might be to simply take until a timer completes:

Observable<String> data = ...
data.takeUntil(Observable.timer(1, TimeUnit.SECOND))
    .subscribe(...);

1 Comment

But timer here is not restarted every time the source Observable emits an item, in contrast to timeout.
4

You can always use onErrorResumeNext which will get the error and you can emit whatever item you want-

    /**
 * Here we can see how onErrorResumeNext works and emit an item in case that an error occur in the pipeline and an exception is propagated
 */
@Test
public void observableOnErrorResumeNext() {
    Subscription subscription = Observable.just(null)
                                          .map(Object::toString)
                                          .doOnError(failure -> System.out.println("Error:" + failure.getCause()))
                                          .retryWhen(errors -> errors.doOnNext(o -> count++)
                                                                     .flatMap(t -> count > 3 ? Observable.error(t) : Observable.just(null)),
                                                     Schedulers.newThread())
                                          .onErrorResumeNext(t -> {
                                              System.out.println("Error after all retries:" + t.getCause());
                                              return Observable.just("I save the world for extinction!");
                                          })
                                          .subscribe(s -> System.out.println(s));
    new TestSubscriber((Observer) subscription).awaitTerminalEvent(500, TimeUnit.MILLISECONDS);
}

2 Comments

I would like to complete stream after onError
you will, in onErrorResumenNext you will emit your Observable which it will finish the pipeline. Run the test that I wrote and you will understand. If you want to see more practical examples check here github.com/politrons/reactive

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.