Reputation: 1462
I have a simple sttp
client implementation:
basicRequest.auth
.get(...)
.response(asJsonEither[GeneralTrait, MyResponse])
.send(backend)
.map(_.body.left.map {
case HttpError(body, statusCode) => mapToTraitImplementation(statusCode, body)
case DeserializationException(_, _) => SomeSerializationError(s"Deserialization error")
})
.flatMap(ZIO.fromEither(_))
GeneralTrait
looks like:
sealed trait GeneralTrait extends Throwable
And it has few same structure implementations:
final case class FirstError(msg: String, code: Int) extends GeneralTrait
But when I run this client, I always get an error DeserializationException
because it could not parse error response to GeneralTrait
. When I changed this:
.response(asJsonEither[GeneralTrait, MyResponse])
to
.response(asJsonEither[FirstError, MyResponse])
everything is mapped correctly and it does not return DeserializationException
, it chooses first case HttpError(body, statusCode) =>
.
But I have multiple implementations of this trait and I would like to have trait
here if it is possible:
.response(asJsonEither[GeneralTrait, MyResponse])
How should I fix this? Is it even possible to do that?
EDIT:
I'm using Circe
serialization. I also added Codecs
:
implicit val decoderGeneralTrait: Codec[GeneralTrait] = deriveConfiguredCodec
implicit val decoderFirstError: Codec[FirstError] = deriveConfiguredCodec
Upvotes: 1
Views: 264