简体   繁体   中英

Mixing Eithers and other exceptions inside an either.eager

I have code I've written inside an either.eager

return either.eager<MyException, MyResponse> {

    val objList = service.getObjs().bind()
    val obj = objList.find{...} ?: throw MyException.notFoundException()

    someOtherService.doSomethingWith(obj).bind()
}
.fold(
    ...
)

My question is:

Is this the idiomatic way to handle errors within an either.eager? ie is it ok to throw an exception from inside an eager?

I could create a function to do the find and return an Either there but that isn't exactly the kotlin idiomatic way.

Each EffectScope and EagerEffectScope expose shift , where you can short-circuit to R in this case MyException .

When find returns a Nullable Type you can use ensureNotNull , which uses shift underneath.

return either.eager<MyException, MyResponse> {

    val objList = service.getObjs().bind()
    val obj = ensureNotNull(objList.find{...}) { MyException.notFoundException() }

    someOtherService.doSomethingWith(obj).bind()
}
.fold(
    ...
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM