I’m trying to create a custom Consumer
(RxJava) implementation that will handle some exceptions (i.e. InternetConnectionException
).
I’m using MVP and when I get InternetConnectionException
I need to call mViewDelegate.onError(throwable.getMessage())
and I want to pass that method as onError: () -> Unit
to my consumer.
What I want to achieve looks like this:
new TestConsumer() {
@Override
public void accept(Throwable throwable) throws Exception {
super.accept(throwable, () -> mViewDelegate.onError(throwable.getMessage()));
}
What I came up with is this:
abstract class TestConsumer : Consumer<Throwable> {
lateinit var onError: () -> Unit
@Throws(Exception::class)
override fun accept(throwable: Throwable?) {
throwable?.let {
if (throwable is InternetConnectionException) {
onError()
} else {
throw throwable
}
}
}
}
but I also need to set onError
like this:
new TestConsumer() {
@Override
public void accept(Throwable throwable) throws Exception {
super.accept(throwable);
super.onError = () -> mViewDelegate.onError(throwable.getMessage());
}
Are there any better approaches?