Has anyone created a library for ktor client possible response types? I'm basically thinking of having Kotlin's Result class, but a bit more fine grained in terms of failure cases (network failure (like airplane mode) vs a server failure for example)
💯 3
👀 2
s
Sam
05/28/2024, 8:36 AM
I would love it if Ktor returned failure results instead of throwing exceptions 😍
Sam
05/28/2024, 8:38 AM
I did try writing something like this for my last project, but I found it virtually impossible to work with a hierarchy of failure result types when there are no union types available.
Sam
05/28/2024, 8:40 AM
If/when union types are finally added to the language, I'm really hoping to see Kotlin and its libraries start to move towards using result types as standard instead of exceptions 🙏
s
Stylianos Gakis
05/28/2024, 8:57 AM
Mapping all ktor internal exceptions to one sealed hierarchy should still work shouldn't it?
Then to merge those with whatever your own domain's errors might look like would be a per-project situation of course.
c
Colton Idle
05/29/2024, 6:13 PM
I tried... but ended up failing trying to have a very simple version of
Copy code
when (val result = myApi.someEndpoint()) {
is Success -> doSomethingWith(result.response)
is Failure -> when (result) {
is NetworkFailure -> showError(result.error)
is HttpFailure -> showError(result.code)
is ApiFailure -> showError(result.error)
is UnknownFailure -> showError(result.error)
}
}
^ inspired by slack's eithernet library.
but man... just doing result.when then hitting alt + enter and getting all of the success and failure cases was so nice.