I'm refactoring an app to reactive paradigm using RxJava. I'm doing it step by step so I need to use toBlocking()
in some cases in order to respect the interfaces, for the moment. How can I handle an error when use toBlocking()
?
Before, I had something like this:
public List<Employee> getEmployees() {
try {
return repository.getEmployees();
} catch(Exception e) {
throw new MyCustomException();
}
}
Now, the repository has a reactive interface (returns Observable<List<Employee>>
), so I'm doing this:
public List<Employee> getEmployees() {
return repository.getEmployees().toBlocking().single();
}
Subscription to repository.getEmployees()
may return an error Observable
. My question is: how can I handle this error keeping it blocking?
I have found out a method singleOrDefault()
, but something like singleOrThrow(new MyCustomException())
would be nice.