Hi All, another question: I'm using `Either` like ...
# arrow
x
Hi All, another question: I'm using
Either
like this:
Copy code
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)?
r
Copy code
Either.catch { } //inside suspend
🙏 1
s
Catching all exceptions is usually not a good idea. You’d usually want to catch non-fatal exceptions and handle them. Fatal exceptions should crash your app (fast failure). Also, I tend to not use Exception as the left-type/error-type. Try to model your errors, by examining and translating the caught exception into a (data) class that represents the error you want to return to the caller.
Copy code
return try {
    Either.Right(someResult(...))
} catch (e: SpecificException) {
    Either.Left(MyError.from(e))
}
👍 1
☝️ 1
Catching any exception in
suspend
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.
👌 1
r
👍 1
@streetsofboston I believe the new Arrow Fx Coroutine lib currently in PR does not face that problem since we are not dealing with exceptions there but @simon.vergauwen can confirm better than I do.
s
Catching any exception in 
suspend
 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.
That is correct but it’s a problem introduces by KotlinX not something that inherit-ally comes from Java or Kotlin itself.
We never catch fatal-exceptions in Arrow, and if we do so we consider that a bug.
s
@raulraja BTW: I really enjoyed your KUG Chicago talk you recently gave about type-proofs
Yes, catching the CancellationException being bad is something unique to kotlinx-coroutines. But it easy to make that mistake there and can have bad side-effects…. But my main point is that I think that is it better to not return exceptions as error-types (left-types) but your own model error-types instead.
👍 1
💯 1
☝️ 1
r
thanks @streetsofboston we showed more of that at kotliners today, hope the video comes live soon
s
I saw the video as a recording at a later time, not live during your talk. The video is available to watch for anyone, i think
r
do you have a link? Can’t find it
s
Hard time finding it back, it wasn’t on youtube….
@raulraja Not sure why it didn’t show up in my history, but the chicago talk is right on the 47 Degrees Youtube site:

https://www.youtube.com/watch?v=lK80dPcsNUg