Xuc Xiem
06/05/2020, 1:27 PMEither
like this:
fun f(): Either<Exception, A> = try {
// do something
Either.right(a)
}
catch (e: Exception) {
Either.left(e)
}
Is that the correct way to use Either
? Is there anything more suitable than Either for that purpose (wrapping a value and catching all exceptions)?raulraja
06/05/2020, 1:28 PMEither.catch { } //inside suspend
streetsofboston
06/05/2020, 1:41 PMreturn try {
Either.Right(someResult(...))
} catch (e: SpecificException) {
Either.Left(MyError.from(e))
}
streetsofboston
06/05/2020, 1:43 PMsuspend
fun can have bad side-effect because you could catch a CancellationException
that way, which then gets ignored (not re-thrown) and will break the Structured Concurrency framework.raulraja
06/05/2020, 1:45 PMEither.catch
only handles non fatal exceptions that are safe to capture https://github.com/arrow-kt/arrow-core/blob/5b8ca8b74d5c130987c77aedad693849a9d73f72/arrow-core-data/src/main/kotlin/arrow/core/Either.kt#L901raulraja
06/05/2020, 1:48 PMsimon.vergauwen
06/05/2020, 1:49 PMCatching any exception inThat is correct but it’s a problem introduces by KotlinX not something that inherit-ally comes from Java or Kotlin itself.fun can have bad side-effect because you could catch asuspend
that way, which then gets ignored (not re-thrown) and will break the Structured Concurrency framework.CancellationException
simon.vergauwen
06/05/2020, 1:49 PMstreetsofboston
06/05/2020, 1:49 PMstreetsofboston
06/05/2020, 1:51 PMraulraja
06/05/2020, 2:00 PMstreetsofboston
06/05/2020, 2:03 PMraulraja
06/05/2020, 2:05 PMstreetsofboston
06/05/2020, 2:18 PM