The rxlint check for RxJava has been updated to version 1.7.0. In this version there are exciting new features:
support for some OnError*
operators and RxKotlin
and AutoDispose
support!
Support for onError operators
As you are hopefully know, the RxSubscribeOnError
check in rxlint
will flag an error when subscribing to an observable without adding an error handler.
Flagging and preventing these errors is extremely important because debugging these errors at runtime is still very hard. In many projects, not handling errors is the number 1 issue when dealing with RxJava!
There are some cases however where it might be OK to not add an error handler. For example, the onErrorReturnItem()
operator, when added as the last operator before the subscribe()
call, will
cause a default item to be returned instead of throwing an error. Note that when this is not the last operator, then errors might still happen, depending on operators that are added to the stream.
Previously for these situations two options existed:
- Add an error handler anyway
- Ignore the lint error by using the
@SuppressLint
annotation
With the latest release, rxlint will now no longer flag RxSubscribeOnError
for cases that it can be sure that all errors are swallowed / converted
by RxJava. This only works when the operator is added as the last operator before the subscribe
call and for the following operators / observables:
io.reactivex.Observable.onErrorReturnItem()
io.reactivex.Flowable.onErrorReturnItem()
io.reactivex.Completable.onErrorComplete()
io.reactivex.Maybe.onErrorReturnItem()
io.reactivex.Maybe.onErrorComplete()
Other onError*
operators that use a function to evaluate the error are not supported, since those functions could throw an error, or pass the error conditionally.
When your code base commonly uses the above operators, then this will save you suppressing the RxSubscribeOnError
from now on.
Support for RxKotlin subscribeBy
RxKotlin
provides Kotlin bindings for RxJava. One of the features is that it adds the subscribeBy
extension function, which
allows to subscribe using lambdas and named parameters. Incidentally it also provides default implementations for these handlers, which makes
it super easy to forget to handle errors :(
If you used subscribeBy
then rxlint
would not flag subscribeBy
without an error handler RxSubscribeOnError
or
code that doesn’t keep a reference to the returned Disposable
with RxLeakedSubscription
. In this version this was fixed, and
all calls to subscribeBy
without an onError
lambda will get flagged with RxSubscribeOnError
, similar to a normal subscribe
call.
The exception for OnError*
operators has also be implemented for subscribeBy
.
Support for AutoDispose
AutoDispose
is a library from Uber that can automatically manage the disposal of subscriptions, based on a lifecycle. Due to the way it works,
if you used AutoDispose with rxlint
, no errors would be flagged for RxSubscribeOnError
or RxLeakedSubscription
. Obviously, RxLeakedSubscription
would be no longer applicable when using AutoDispose, but we would still want to catch potential errors when forgetting an onError handler!
In the latest release this now works as you’d expect, and the exception for onError*
operators is also implemented for AutoDispose
.
Getting rxlint
Updating, or adding rxlint
to your project is super easy, just add the rxlint
dependency to your project: implementation 'nl.littlerobots.rxlint:rxlint:1.7.0'
and you’re good to go.
Saying goodbye to RxJava 1.x
One thing to note is that I’m no longer adding new checks for RxJava 1.x. In the 2.0.0 release of rxlint
(whenever that will be) RxJava 1.x support will be removed.
Existing checks will keep working for RxJava 1.x in the meantime.
I hope you’ll enjoy these updates and get rid of pesky bugs when using RxJava!