LoginSignup
0
0

More than 5 years have passed since last update.

RxJavaでretryWhenを使ってエラーをキャッチして別のエラーを流す

Posted at

この辺の話に近い。

Observable.create<Int> { emitter ->
    emitter.onNext(1)
    emitter.onNext(2)
    emitter.onError(HogeException())
    emitter.onComplete()
}.retryWhen { observable ->
    observable.flatMap { e ->
        val exception = if (e is HogeException) FooException() else e
        Observable.error<Int>(exception)
    }
}.subscribe({
    println(it)
}, { e ->
    println(e.javaClass.simpleName)
}, {})

class HogeException : RuntimeException()
class FooException : RuntimeException()

こんな感じでHogeExceptionが流れてきたら、FooExceptionにExceptionを変更して流す。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0