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? i.e 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(
...
)