Starting to get into arrow to improve error handli...
# arrow
c
Starting to get into arrow to improve error handling. Have methods that may throw an exception (somewhere in their call hierarchy) - struggling with how to turn those into an Either. Have this so far, seems clunky:
Copy code
private fun someFun() : Either<String,Int> {
        return either.eager {
            Either.catch({ it.message ?: "No message" }) {
                42 // this would be a call hierarchy that may throw exceptions
            }.bind()  // Hmmm.  need to unwrap either created from 'catch' to get a value for 'eager'
        }  
    }
s
Isn’t
either.eager { Either.catch(...).bind() }
the same as just
Either.catch(...)
in this case?
s
Hey @Chris Lee, Typically you would indeed use
Either.catch
+
mapLeft
(+
bind
) but in your case you you don't need
either.eager
nor
bind
.
Copy code
private fun sampleFun(): Either<String, Int> =
  Either.catch { 42 }
    .mapLeft { it.message ?: "No message" }
c
yes,
either.eager
isn’t needed in this trivial example, but is required if you want to use
ensure()
,
ensureNotNull
, or are calling other methods that return eithers to be able to
bind()
them.
s
Right, I'm currently trying to backport the 2.x.x API to 1.x.x. I think it currently would look like this in 2.x.x
Copy code
either {
  val res = catch({ 42 }) { 
    raise(it.message ?: "No message")
  }
}
This API mimics
Flow#catch
where you can return a default value or raise another error.
The bind happens automatically in the DSL
c
ah cool. will have a look at 2.x.x stuff to orient there, and patiently await this API 😉
s
I'm currently back-porting it to 1.1.x, so might be here pretty soon 🤞That would also make the migration from 1.x.x to 2.x.x much smaller 🤞
c
cool. thanks for the info. 🙏