Hi folks. I'm currently using ktor-client with arr...
# ktor
c
Hi folks. I'm currently using ktor-client with arrow-kt, and I have an interface like this
Copy code
interface MyApi {
    // NetworkError is a sealed interface
    suspend fun getFoo(): Either<NetworkError, FooDto>
}
My intention is that in case there is a request exception or response error,
getFoo()
returns
Left
. Thanks to
createClientPlugin()
, I successfully transformed response errors to
NetworkError
but have yet to transform request exceptions. So currently
MyApiImpl
is looking like below, which is repetitive and error-prone
Copy code
class MyApiImpl(val client: HttpClient) : MyApi {
    override suspend fun getFoo() = Either.catch {
        client.get("foo")
     }
        .mapLeftNetworkError()
}
My question is, is there any way I get rid of
Either.catch { ... }.mapLeftNetworkError()
expression by just writing
client.get("foo")
?
a
I think you can write a serializer for the
Either<NetworkError, FooDto>
and use the
ContentNegotiation
plugin to receive objects of that type. You can read about custom serializers in the documentation of the kotlinx.serialization library.
🙌 1