Mutiny how to interupt chain operation

53 views Asked by At

I'm using Kotlin and Quarkus to building restful api , I wanna know how to interrupt the chain operation when there is some error. say we have code below:

fun asyncOperation1(): Uni<String> {}

fun asyncOperation2(): Uni<String> {}

fun asyncOperation3(): Uni<String> {}

@POST
@Path("/somepath")
@ReactiveTransactional
fun m1(payload: Payload): Uni<MyResponse<String>> {
    val uniRes1 = asyncOperation1()
    val uniRes2 = asyncOperation2()
    val uniRes3 = asyncOperation3()

    return Uni.combine().all().unis(uniRes1, uniRes2, uniRes3).combinedWith { str1, str2, str3 ->
        val arr = listOf(str1, str2, str3)
        for (item in arr) {
            if(item == "invalid string"){
                return Uni.createFrom().failure("Invalid String")
                // throw Exception()
            }
        }
        arr
    }.chain { arr -> 
       // ...
    }.onFailure {
        return Uni.createFrom().Item(Myresponse.error("invalid"))
    }
    .chain{} // further operaion
    .chain{
         return Uni.createFrom().Item(Myresponse.Ok("Success"))
     }
}

the code is for demostration. Here I have three async operation , and I wanna check if any of the results is "invalid string", if any I'd prefer stop the chain operaion immediately and return the HttpResponse to frontend(return at the OnFailure method and no more further operation).if there is not, then pass the list of results to the further operation.

the docs says we could create failed Uni instances with Uni.createFrom().failure(new Exception("boom")), could I just throw a runtimeExcepition?

is the code above the right way to handle the error in Mutiny?

0

There are 0 answers