1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RxTracerでエラーを追跡しやすくする

Posted at

redditのandroiddevに、RxJava 2関連のライブラリ RxTracer が取り上げられていたので、使ってみました。
RxTracerはRxJava 2のスタックトレースに、subscribe側を含むように書き換えるライブラリです。

halfhp/rxtracer: Better stack traces for RxJava2
https://github.com/halfhp/rxtracer

使い方

RxTracerの使い方はとても簡単です。
ライブラリをプロジェクトに追加した後は、有効化するだけです。

RxTracer.enable()

RxTracer適用前後で比較する

RxTracerを適用する前後で、例外発生時のバックスタックがどう変わるか見てみましょう。

動かすのは、以下のコードです。
IllegalArgumentExceptionを発生させ、わざとOnErrorNotImplementationExceptionに繋げます。

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    tracer().subscribe()
}

private fun tracer() : Observable<String> {
    return Observable.fromCallable {
        throw  IllegalArgumentException()
        return@fromCallable "aaaa"
    }
    .subscribeOn(Schedulers.io())
}

RxTracer適用前

RxTracerを適用していない場合のスタックトレースは以下のとおりです。
非常に長いので、画像のキャプチャにしました。

Screen Shot 2018-05-13 at 19.52.27.png

スタックトレースには、IllegalArgumentExceptionの発生箇所しか含まれていません。
なお、subscribeOnを消すと、RxTracerが適用されてなくてもsubscribeを含んだログが含まれます。

RxTracer適用後

次に、onCreate内の先頭でRxTracerを有効にした場合です。
(通常だと、デバッグ用のApplicationクラス内などで有効化するでしょう。)

Screen Shot 2018-05-13 at 19.55.30.png

スタックトレースに、onCreate内のsubscribeの行を含んでいます。
どこがOnErrorNotImplementationExceptionでエラーの実装が足りてないのか分かりやすくなりました。

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?