Skip to content

Commit

Permalink
Merge pull request #1373 from ingarabr/feature/http-client-debug-info
Browse files Browse the repository at this point in the history
add context to json paring errors
  • Loading branch information
fthomas committed Mar 25, 2020
2 parents b4df9c0 + 40fd2fa commit 77925ac
Showing 1 changed file with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import io.circe.{Decoder, Encoder}
import org.http4s.Method.{GET, POST}
import org.http4s.circe.{jsonEncoderOf, jsonOf}
import org.http4s.client.Client
import org.http4s.{Headers, Method, Request, Response, Status, Uri}
import org.http4s.{DecodeFailure, Headers, HttpVersion, Method, Request, Response, Status, Uri}

import scala.util.control.NoStackTrace

Expand All @@ -43,7 +43,7 @@ final class HttpJsonClient[F[_]: Sync](
private def request[A: Decoder](method: Method, uri: Uri, modify: ModReq): F[A] =
client.expectOr[A](modify(Request[F](method, uri)))(resp =>
toUnexpectedResponse(uri, method, resp)
)(jsonOf[F, A])
)(jsonOf[F, A].transform(_.leftMap(failure => JsonParseError(uri, method, failure))))

private def toUnexpectedResponse(
uri: Uri,
Expand All @@ -55,6 +55,17 @@ final class HttpJsonClient[F[_]: Sync](
}
}

final case class JsonParseError(
uri: Uri,
method: Method,
underlying: DecodeFailure
) extends DecodeFailure {
val message = s"uri: $uri\nmethod: $method\nmessage: ${underlying.message}"
override def cause: Option[Throwable] = underlying.some
override def toHttpResponse[F[_]](httpVersion: HttpVersion): Response[F] =
underlying.toHttpResponse(httpVersion)
}

final case class UnexpectedResponse(
uri: Uri,
method: Method,
Expand Down

0 comments on commit 77925ac

Please sign in to comment.