Hexa
02/24/2019, 9:05 AMtry catch
override fun f1(): Either<Errors.ApplicationError, AWSClient> =
Try {
try {
// logic1 here
} catch (ex: Exception) {
// if logic1 throws exception then try logic2 in here
}
}.toEither().mapLeft {
(Errors.UnexpectedError("Unexpected error: ${it}"))
}
kartoffelsup
02/24/2019, 10:19 AMkotlin
fun f1(): IO<AWSClient> = IO {
tryToConnect()
}.handleErrorWith { ignored: Throwable ->
IO { tryADifferentApproach() }
}
and then use the AWSClient inside the IO via map and flatMapkioba
02/24/2019, 1:03 PMTry
has the capability to recovery form exception recoverWith
and it also safe agains exception if you return a Try
again.kioba
02/24/2019, 1:06 PMfun f1() = Try{ logic1() }
.recoverWith{ Try{ logic2() } }
.toEither()
Hexa
02/24/2019, 3:37 PMraulraja
02/25/2019, 9:35 AMTry
unless the function is already suspended. Try can't support side effects suspension and is therefore impure. IO is the best option here as @kartoffelsup pointed out.
Try { boom() } // runs `boom()`
IO { boom() } // suspends `boom()` until you are ready to run