【发布时间】:2020-05-03 10:15:24
【问题描述】:
我创建了错误层次结构:
sealed trait MyError extends Throwable
final case class SecondError(msg: String) extends MyError
现在我的http4s 路由中可能会出现这种错误:
case GET -> Root / "things" => for {
response <- things.all.foldM(
error => error match {
case SecondError(_) => InternalServerError(error)
}
...
但我得到编译错误:
could not find implicit value for parameter encoder: io.circe.Encoder[Throwable]
是否可以用circe 和http4s 对Throwable 进行编码?我试着这样做:
implicit def encoderHttpThrowable: EntityEncoder[Env, Throwable] = jsonEncoderOf[Env, Throwable]
但这并没有解决问题。
【问题讨论】:
-
根据记忆,您没有
MyError特征扩展Throwable,而是将Throwable匹配到您的MyErrors 之一。然后你只需要对错误的案例类进行编码 -
您的意思是扩展 Error 而不是 Throwable?如果我使用的函数需要 Throwable 签名怎么办?
-
在“世界末日”,你有一个
Throwable => MyError的函数,然后circe 对其结果进行编码 -
我有自定义错误的编码器。问题仅在于 Throwable
标签: json scala circe http4s zio