1

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
Developus
  • 1,400
  • 2
  • 14
  • 50
  • Which JSON library are you using, and how is the deserializer for `GeneralTrait` defined? – adamw Mar 14 '23 at 09:42
  • Circe. I edited post and showed how I added decoders – Developus Mar 14 '23 at 09:48
  • Can you try using `decoderGeneralTrait` to decode an example response and checking if this works correctly? – adamw Mar 14 '23 at 10:39
  • I used it. I have added these implicits to client code and tried to get response as `.response(asJsonEither[GeneralTrait, MyResponse])`, but it failed. It always returns `DeserializationException`. With concrete implementation it works fine. – Developus Mar 14 '23 at 10:53
  • @adamw any other suggestions/solutions? – Developus Mar 15 '23 at 16:55
  • 1
    Hm could you create a small reproducible example and report as a bug in https://github.com/softwaremill/tapir? Will be easier to investigate :) – adamw Mar 16 '23 at 08:06
  • Ok, thanks. I reported it https://github.com/softwaremill/tapir/issues/2789 – Developus Mar 16 '23 at 09:55

0 Answers0