hi guys. sorry for this very simple question. I've...
# arrow
g
hi guys. sorry for this very simple question. I've just started using Retrofit and I discovered Arrow last week, so I wanted to ask you a little tip. I have a retrofit api called getUser. Would it make sense to use Either in this way?
class CustomError {
data class RetrofitError(val error: Throwable) : CustomError()
// Add other custom errors if needed
}
suspend fun getUser(): Either<CustomError, User> {
return try {
val response: Response<User> = retrofitApi.getUser()
if (response.isSuccessful) {
response.body()?.right() ?: CustomError.RetrofitError(Throwable("No user found")).left()
} else {
CustomError.RetrofitError(Throwable("Response not successful")).left()
}
} catch (e: Exception) {
CustomError.RetrofitError(e).left()
}
}
d
Did you see: io.arrow-kt:arrow-core-retrofit
g
oh no, I didnt know that
Dave, by chance are you aware of android project using arrow-core-retro? Could nt find anything on github
g
thank you, very kind of you. 🙂
d
I forgot to mention this one (the one I'm currently using): https://github.com/arrow-kt/arrow/tree/main/arrow-libs/core/arrow-core-retrofit
👍 1
g
thanks again. I m watching some youtube presentation. Afters decades of imperative programming, it is not easy to switch, but I really like it
c
Also note 2 nice arrow features: 1. This will do what your code does but not capture any exceptions it shouldn't such as interrupt or cancellation exceptions
Copy code
fun example() = Either.catch { ... }
2. Using the raise API you can unwrap eithers sort of akin to async await
Copy code
fun example(): Either<String, String> = either {
  val x = somethingReturningEither().bind()
  val y = somethingElseWithDifferentLeft().mapLeft { "y failed" }.bind()

  ensureNotNull(x) { "Missing my x!" }
  x + y
}
❤️ 1
g
thank you very much, the more i look at this framework, the more i like it. impressive. next step will be "typed errors" and validation :-)
👍 1