Skip to content

Commit

Permalink
Add more detail to exceptions thrown in the client interpreter (#4085)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw authored Oct 7, 2024
1 parent 54c1d8a commit 18a63ed
Showing 1 changed file with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,16 @@ trait SttpClientInterpreter extends SttpClientInterpreterExtensions {
def toRequestThrowErrors[I, E, O, R](e: PublicEndpoint[I, E, O, R], baseUri: Option[Uri])(implicit
wsToPipe: WebSocketToPipe[R]
): I => Request[O, R] =
i =>
new EndpointToSttpClient(sttpClientOptions, wsToPipe)
.toSttpRequest(e, baseUri)
.apply(())
.apply(i)
i => {
val request = new EndpointToSttpClient(sttpClientOptions, wsToPipe).toSttpRequest(e, baseUri).apply(()).apply(i)
request
.mapResponse(throwDecodeFailures)
.mapResponse {
case Left(t: Throwable) => throw new RuntimeException(throwErrorExceptionMsg(e, i, t.asInstanceOf[E]), t)
case Left(t) => throw new RuntimeException(throwErrorExceptionMsg(e, i, t))
case Left(t: Throwable) => throw new RuntimeException(throwErrorExceptionMsg(e, i, t.asInstanceOf[E], request), t)
case Left(t) => throw new RuntimeException(throwErrorExceptionMsg(e, i, t, request))
case Right(o) => o
}
}

// secure

Expand Down Expand Up @@ -192,17 +191,16 @@ trait SttpClientInterpreter extends SttpClientInterpreterExtensions {
wsToPipe: WebSocketToPipe[R]
): A => I => Request[O, R] =
a =>
i =>
new EndpointToSttpClient(sttpClientOptions, wsToPipe)
.toSttpRequest(e, baseUri)
.apply(a)
.apply(i)
i => {
val request = new EndpointToSttpClient(sttpClientOptions, wsToPipe).toSttpRequest(e, baseUri).apply(a).apply(i)
request
.mapResponse(throwDecodeFailures)
.mapResponse {
case Left(t: Throwable) => throw new RuntimeException(throwErrorExceptionMsg(e, a, i, t.asInstanceOf[E]), t)
case Left(t) => throw new RuntimeException(throwErrorExceptionMsg(e, a, i, t))
case Left(t: Throwable) => throw new RuntimeException(throwErrorExceptionMsg(e, a, i, t.asInstanceOf[E], request), t)
case Left(t) => throw new RuntimeException(throwErrorExceptionMsg(e, a, i, t, request))
case Right(o) => o
}
}

//

Expand All @@ -213,11 +211,11 @@ trait SttpClientInterpreter extends SttpClientInterpreterExtensions {
case f => throw new IllegalArgumentException(s"Cannot decode: $f")
}

private def throwErrorExceptionMsg[I, E, O, R](endpoint: PublicEndpoint[I, E, O, R], i: I, e: E): String =
s"Endpoint ${endpoint.show} returned error: $e, inputs: $i."
private def throwErrorExceptionMsg[I, E, O, R](endpoint: PublicEndpoint[I, E, O, R], i: I, e: E, r: Request[_, _]): String =
s"Endpoint ${endpoint.show} returned error: $e, inputs: $i. Request: ${r.showBasic}."

private def throwErrorExceptionMsg[A, I, E, O, R](endpoint: Endpoint[A, I, E, O, R], a: A, i: I, e: E): String =
s"Endpoint ${endpoint.show} returned error: $e, for security inputs: $a, inputs: $i."
private def throwErrorExceptionMsg[A, I, E, O, R](endpoint: Endpoint[A, I, E, O, R], a: A, i: I, e: E, r: Request[_, _]): String =
s"Endpoint ${endpoint.show} returned error: $e, for security inputs: $a, inputs: $i. Request: ${r.showBasic}."
}

object SttpClientInterpreter {
Expand Down

0 comments on commit 18a63ed

Please sign in to comment.