Has anyone created a library for ktor client possi...
# ktor
c
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
I would love it if Ktor returned failure results instead of throwing exceptions 😍
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.
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
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
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.