1

我有错误模型,例如:

sealed trait HttpError {
    val msg: String
    val cause: String
  }

  final case class HttpDecodingError(cause: String) extends HttpError {
    override val msg: String = "Decoding error"
  }

  final case class HttpInternalServerError(msg: String, cause: String) extends HttpError
  case object HttpUnauthorizedError extends HttpError {
    override val msg: String = "Invalid credentials"
    override val cause: String = ""
  }
  final case class HttpBadRequestError(msg: String, cause: String) extends HttpError

在我的路线中,我基于此模型生成 http 错误类型,即:

.foldM(
          {
            case error: HttpDecodingError       => BadRequest(error.asInstanceOf[HttpError])
            case error: HttpInternalServerError => InternalServerError(error.asInstanceOf[HttpError])
            case HttpUnauthorizedError          => Unauthorized(withChallenge("Invalid credentials"))
            case error: HttpBadRequestError     => BadRequest(error.asInstanceOf[HttpError])
          },
          Ok(_)
        )

但问题是我需要添加这个 asInstanceOf,否则 circe 看不到编码器。我的编码器看起来像:

implicit val encodeHttpError: Encoder[HttpError] = (error: HttpError) =>
    Json.obj(("msg", Json.fromString(error.msg)), ("cause", Json.fromString(error.cause)))

有没有办法避免在那里做 asInstanceOf ?

4

1 回答 1

3

您不能将编码器HttpError用于它的子类,因为Encoder它是不变的(如果它是协变的,它将起作用)。

您可以使用的一种解决方案是使用参数化def而不是定义编码器val

implicit def encodeHttpError[E <: HttpError]: Encoder[E] = (error: E) =>
  Json.obj(
    ("msg", Json.fromString(error.msg)),
    ("cause", Json.fromString(error.cause))
  )

这样,您将拥有所有子类型的编码器实例HttpError以及HttpError.

于 2021-01-20T12:49:32.850 回答