cavega
02/12/2021, 4:03 AMEither
such that if Left
meets some specific condition (i.e. filter out some error code that should not throw an error) the downstream Either via the fold operation gets routed to ifRight
?
I did something like this (where Either<List<MyErrors>, <Data>>):
return someService()
.map { it.toEither().mapLeft {
it.filter { it.code != "some-failure-code" }
} }
But all that does in return an empty list of MyErrors to the downstream fold’s isLeft.Scott Christopher
02/12/2021, 6:28 AMLeft
potentially to a Right
depending on its value, you can make use of handleErrorWith
- https://arrow-kt.io/docs/arrow/typeclasses/applicativeerror/#kindf-ahandleerrorwithScott Christopher
02/12/2021, 6:34 AMsomeService()
.map { it.toEither().handleErrorWith {
when (val errs = it.filter { it.code != "some-failure-code" }) {
errs.isEmpty() -> Right(Unit) // or some other value to recover to
else -> Left(errs)
}
} }
simon.vergauwen
02/12/2021, 9:46 AMLeft
into Right
by inspecting Left
and returning any arbitrary Either
using handleErrorWith
.
Or use handleError
if you only want to return a Right
.
val either: Either<String, Int> = ...
either.handleErrorWith { str ->
Either.catch { str.toInt() }
}
either.handleError { str -> 0 }
cavega
02/12/2021, 2:21 PM