From 18a63ed156622056fbe1af0710951320a37b79b7 Mon Sep 17 00:00:00 2001 From: Adam Warski Date: Mon, 7 Oct 2024 16:14:12 +0200 Subject: [PATCH] Add more detail to exceptions thrown in the client interpreter (#4085) --- .../client/sttp/SttpClientInterpreter.scala | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/client/sttp-client/src/main/scala/sttp/tapir/client/sttp/SttpClientInterpreter.scala b/client/sttp-client/src/main/scala/sttp/tapir/client/sttp/SttpClientInterpreter.scala index 651f208841..c110b10785 100644 --- a/client/sttp-client/src/main/scala/sttp/tapir/client/sttp/SttpClientInterpreter.scala +++ b/client/sttp-client/src/main/scala/sttp/tapir/client/sttp/SttpClientInterpreter.scala @@ -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 @@ -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 } + } // @@ -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 {