Retrieving source observable after exception in RxJava.

some immediate thoughts/notes:

  1. you say "Using rxjava I get the short url from my AsyncTask in onNext.." are you using an AsyncTask to get the short url? if yes, you should avoid that and use RxJava for that too as it would give you more control. If that is beyond your control i.e. it's some external AsyncTask api, then never mind.
  2. Also, Dan Lew on an episode of Fragmented pointed out that "onError" is when something has gone catastrophically wrong. Not for logical errors or business logic fails. Your use case definitely rings as a BL fail vs onError. So i would avoid onError if this call as(BITLY_USER, BITLY_API_KEY).call(shorten(urlToBeShortened)).getShortUrl() is something that you control.
  3. Observable.create is pretty verbose and is used more in cases where you're trying to create an observable that does something fancy vs executing a chain of existing operators. .just or .from are better starting observables in cases where the initial emission is instant.
  4. You're code actually looks ok, but i see a lot of unnecessary nesting. Maybe i'm not understanding your use case precisely.

Here's a scratchpad version (i haven't tested or even checked this for syntax, so YMMV):

```

Observable.just(urlToBeShortened) .flatMap(new Func1<String, Observable<String>>() { @Override public Observable<String> call(String bitlyUrl) { // I'm guessing this is an async task? return as(BITLY_USER, BITLY_API_KEY).call(shorten(urlToBeShortened)).getShortUrl(); } }) .onErrorResumeNext(new Func1<Throwable, Observable<? extends String>>() { @Override public Observable<? extends String> call(Throwable throwable) { Log.i("onErrorResumeNext", "message " + throwable.getMessage()); if (throwable instanceof BitlyTask.AlreadyABitLyLinkException) { return Observable.just(((BitlyTask.AlreadyABitLyLinkException) throwable).getSourceUrl()); } else { return Observable.empty(); } } }); ```

/r/androiddev Thread