Partho Paul
06/22/2022, 11:16 AMfun f1(): Either<Throwable, String> {
either {
doSomething() //throws exception
}
}
fun f2() {
f1().fold(
{
print("you messed up")
doErrorHandling()
},
{
print("everything's fine")
}
)
}
fun main() {
runCatching{
f2()
}
.onSuccess {
print("it worked")
}
.onFailure {
print("Either didn't catch exception")
}
}
When function f1() throws exception here, the code should print “you messed up” but instead it’s printing “Either didn’t catch exception”. Am I doing anything wrong here? I’m using kotlin 1.7.0 and arrow version 1.1.2
TIAstojan
06/22/2022, 11:28 AMEither.catch {}
or similar functions e.g.
Either.catch { doSomething() }
Partho Paul
06/22/2022, 11:29 AMAlejandro Serrano Mena
06/22/2022, 11:41 AMEither.catch
within an either
block a bit confusingAlejandro Serrano Mena
06/22/2022, 11:42 AMfun f1(): Either<Throwable, String> =
Either.catch { doSomething() }
or you need to call bind
to make the block aware of the thing being run
fun f1(): Either<Throwable, String> {
either {
Either.catch { doSomething() }.bind()
}
}
Partho Paul
06/22/2022, 11:43 AMfun f1(): Either<Throwable, String> {
Either.catch {
doSomething() //throws exception
}
}
fun f2() {
f1().fold(
{
print("you messed up")
doErrorHandling()
},
{
print("everything's fine")
}
)
}
fun main() {
runCatching{
f2()
}
.onSuccess {
print("it worked")
}
.onFailure {
print("Either didn't catch exception")
}
}
Alejandro Serrano Mena
06/22/2022, 11:44 AMfun f1(): Either<Throwable, String> =
Either.catch { doSomething() }
(note that I'm using =
and not braces)Partho Paul
06/22/2022, 12:37 PMfun f1(): Either<Throwable, String> =
Either.catch { doSomething() }
but still not able to catch exception in f2stojan
06/22/2022, 12:40 PMEither.catch
is basically a `try/catch`https://github.com/arrow-kt/arrow/blob/051847de3b1d3186fc9843864aaf642a2f4a854f/ar[…]libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt
except if your exception is fatal e.g.: https://github.com/arrow-kt/arrow/blob/0afdd9a7cc5251cf0c50701b21ccbb5693045bde/arrow-libs/core/arrow-core/src/jvmMain/kotlin/arrow/core/NonFatal.kt#L10Partho Paul
06/22/2022, 12:55 PMraulraja
06/22/2022, 2:51 PMLinkageError
, how did you plan to recover from it? and what leads you to this point where you care about it in user code?Partho Paul
06/22/2022, 3:39 PMLinkageError
. I’m migrating a legacy codebase into a new microservice. I’ll discuss it with my colleagues if we should continue the earlier workflow or short-circuit the process. I’ll be in favour of short-circuiting as continuing the earlier workflow will make the system less reliable.raulraja
06/22/2022, 4:53 PMLinkageError
is fatal and usually means you are missing a native library file in the path and your application should not even be running at that point. Exceptions would be some kind of local application where the user is responsible to provide the linked libraries manually but that is an odd case. Normally you can’t recover from fatal exceptions because at that point for many of them you don’t even have guarantees the VM is in a recoverable state, for example OutOfMemoryError