I'm looking through this example and wonder if Exception handling is intentionally omitted here (all the way down the stack)? My assumption is that the function is defined as
suspend
because it is making a network call (not pure), but I'm curious what would be the recommended approach to handling exceptions.
r
raulraja
02/15/2022, 5:04 PM
hi @Srki Rakic, you can use
Either.catch { }
then
toValidatedNel
raulraja
02/15/2022, 5:04 PM
if you want to handle exception resulting from the network call and make them part of the returned value
raulraja
02/15/2022, 5:06 PM
One of your error cases for validation may look like.
Copy code
data class InvalidDue2Network(t: Throwable): YourBaseError()
raulraja
02/15/2022, 5:07 PM
so you can go from the network throwable to your model before exiting the function.
s
Srki Rakic
02/15/2022, 6:26 PM
Thank you. That's what I typically do with those type of calls
Either.catch { }.mapLeft { }
, but seeing the example I was wondering if that is not the best/recommended pattern for handling functions with side-effects.
s
simon.vergauwen
02/17/2022, 10:22 AM
Either.catch { }.mapLeft { }
is a great pattern to use. Typically you want to make sure that your error type keeps a reference to the
Throwable
so you have that information for later inspection.
Something like:
Copy code
data class DatabaseError(val original: Throwable)
val user: Eithre<Throwable, User> =
Either.catch {
userFromDatabase()
}.mapLeft(::DatabaseError)