What would be the equivalent of `onLeft {}` when w...
# arrow
s
What would be the equivalent of
onLeft {}
when working with the Raise DSL? Given the following using when dealing with an API that exposes `Either`s:
Copy code
fun getIntFromDb(): Either<DbError, Int> = 0.right()
One could “forward” the
Either
and also intercept `Left`s for logging or whatever:
Copy code
fun getInt(): Either<DbError, Int> =
  getIntFromDb()
    .onLeft { log(it.message) }
However, if the API uses the
Raise
DSL, it feels a little more weird:
Copy code
fun Raise<DbError>.getIntFromDb(): Int = 0

fun Raise<DbError>.getInt(): Int =
  withError({
    log(it.message)
    it
  }) { getIntFromDb() }
While writing this, I realized
also
could clear some things up, but I'm still curious if this is the Correct™ approach:
Copy code
fun Raise<DbError>.getInt(): Int =
  withError({ it.also { log(it.message) } }) { getIntFromDb() }
I guess it's also quite convenient to move between the Either and Raise DSLs, so something like this is also definitely a possibility:
Copy code
fun Raise<DbError>.getInt(): Int =
  either { getIntFromDb() }
    .onLeft { log(it.message) }
    .bind()
Feel like I should've thought about that before asking. rubber duck
y
You could roll your own
onError
I guess. The only API I know that does something similar is
traced
, which is useful if you want to log the stack trace as well.
a
I think this is the best you can do with the Raise DSL, indeed. We decided not to include functions to "just inspect" the error in the API, so you need to roll your own
it.also { ... }
. Feel free to open a discussion in the repo; if more people have this use case, we're happy to explore ways to include it in Arrow 2.0 🙂
s
I think I'm fine with moving between
Raise
and
Either
using
either {}
and
bind()
, tbh., but if you think it's worth it, I'd be happy to start a discussion. 👍
1
🙏 1
w
What about something like this?
Copy code
fun Raise<DbError>.getInt(): Int =
  either { getIntFromDb() }
    .onLeft { log(it.message }
    .bind()
LOL, sorry, I missed the message with that exact code 😞.
😉 1
s
Yeah, that's what we've ended up with doing in most cases. 👍