Eldelshell
Eldelshell

Reputation: 6960

Handle onFailure from all streams

In the following scenario, if an error happens on doSomething the onFailure works correctly:

this.restClient.getSomething()
  .flatMap((something) -> {
    return this.restClient.doSomething();
  })
  .flatMap((res) -> {
    return Uni.createFrom().item(Response.ok().build());
  }).onFailure().transform((e) -> {
    LOG.error("Failed with", e);
    throw new InternalServerErrorException("Failed!");
  });

Problem is, if the error happens on getSomething the onFailure isn't called and I receive:

Unhandled asynchronous exception, sending back 500

I've tried using onFailure().call, onFailure().retry() and even set the onFailure at the start, in the middle, but nothing seems to work out.

Upvotes: 0

Views: 350

Answers (1)

jzimmerli
jzimmerli

Reputation: 161

If I understand it correctly, getSomething, create the stream. But at creation something go wrong, e.g. the stream is never created.

As the stream is not create, onAnything can not happen, as the stream is not existing. To avoid this behavior, I usually create the stream with voidItem and then map it to the correct creation. So that error in creation will be handled from the stream, and not from creation code.

Example for uni:

Uni.createFrom().voidItem()
   .map( voidItem -> functionForRealValue)
   .onFailure(handleFailure)

This way, you should be able to handle all errors in the stream. It's important to understand, that you need to create the stream, otherwise you can not run onFailure on it.

Upvotes: 2

Related Questions