Search code examples
javamutinyquarkus-reactive

Mutiny; Why is onFailure called when I get an Item?


I'm a bit lost when playing with Mutiny. I have a testfunction that Creates a String uni and when that is succesful I want that it return a 200 OK or when it fails(which it shouldn´t with this simple string creation) a 500 Internal Server Error for now.

I can get it to work by using the onItemOrOnFailure() of mutiny but I'm trying to split up the handling of the success and failure scenario. I see my sout of the onItem() but I still get to the onFailure() and get a 500 response in postman. Why do I go into the onFailure? What am I not understanding? So I expect one or the other but I get into both.

@GET
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public Uni<RestResponse<?>> test() {
    return Uni.createFrom().item("Hello world")
        .onItem().transform(str -> {
            var resp = RestResponse.ok(str);
            System.out.println("In onItem");
            return resp;
        })
        .onFailure().recoverWithNull().replaceWith(() -> {
            System.out.println("In onFailure");
            return RestResponse.status(500);
        });
}

Solution

  • I think I figured it out. Its not the fault of the onFailure, but its just that the replaceWith isnt part of the onFailure. Its just always called as the next part.
    So on success it goes onItem -> transform -> replaceWith and onFailure it goes onFailure -> recoverWithNull -> replaceWith. So If i understand correctly; when using a recoverWith... function you step out of the failure event and continue with the rest of the steps Is this a correct assumption?

    Ah well what a bit of sleep can do:)