congvc-dev
04/16/2024, 9:47 AMinterface 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
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")
?Aleksei Tirman [JB]
04/16/2024, 7:32 PMEither<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.