Reputation: 6132
I'm trying to write a method that does something like this
Mono<A> ma = networkCall(); //this might fail
Mono<Void> mv = ma.map( a -> ....) #some logic to perform with `A`
return mv;
The trick is, ma
might very well fail and then I would want to just log the situation and return a Mono<Void>
that completes with no error.
Looking at the Mono
api I just found onErrorResume
or onErrorReturn
but both would take a function that returns an A
(which I can't fabricate), while I would like to return a Void
.
I would imagine the solution is quite simple, but couldn't quite find the right operations for this.
So, what operations should I apply to ma
to transform it into a Mono<Void>
in case of error?
Upvotes: 4
Views: 5807
Reputation: 72284
I just found onErrorResume or onErrorReturn but both would take a function that returns an A
onErrorReturn()
indeed requires you to return an A
, but onErrorResume()
just requires you to return a Mono<A>
, which can be empty.
So you can use:
doOnNext()
to perform your logic with A
if the call is successful;doOnError()
to log your error if the call is not successful;onErrorResume()
to return an empty resultthen()
to convert the result into a Mono<Void>
.Something like:
networkCall()
.doOnNext(a -> doSomethingWith(a))
.doOnError(e -> e.printStackTrace())
.onErrorResume(e -> Mono.empty())
.then();
Upvotes: 4