From edd2ee51ca635b00bd9ec48adf925b731ea82cf9 Mon Sep 17 00:00:00 2001 From: Felix Dietze Date: Sat, 24 Aug 2024 17:53:09 +0200 Subject: [PATCH 1/3] jsdomClient: don't parse unsuccessful http response bodies, throw status instead --- jsdomClient/src/main/scala/HttpRpcTransport.scala | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/jsdomClient/src/main/scala/HttpRpcTransport.scala b/jsdomClient/src/main/scala/HttpRpcTransport.scala index bcc7601f..1b793347 100644 --- a/jsdomClient/src/main/scala/HttpRpcTransport.scala +++ b/jsdomClient/src/main/scala/HttpRpcTransport.scala @@ -18,10 +18,12 @@ object HttpRpcTransport { def apply[F[_]: Async](config: F[HttpRequestConfig]): RequestTransport[String, F] = new RequestTransport[String, F] { override def apply(request: Request[String]): F[String] = for { - config <- config - url = s"${config.baseUri}/${request.method.traitName}/${request.method.methodName}" + config <- config + url = s"${config.baseUri}/${request.method.traitName}/${request.method.methodName}" requestArgs = new dom.RequestInit { headers = config.headers.toJSDictionary; method = dom.HttpMethod.POST; body = request.payload } - result <- Async[F].fromThenable(Async[F].delay[js.Thenable[String]](dom.fetch(url, requestArgs).`then`[String](_.text()))) + response <- Async[F].fromPromise(Async[F].delay(dom.fetch(url, requestArgs))) + _ <- Async[F].raiseWhen(!response.ok)(new Exception(s"HTTP error ${response.status}")) + result <- Async[F].fromPromise(Async[F].delay(response.text())) } yield result } } From c51949329debacbbfc0020ea487c3797f22f8716 Mon Sep 17 00:00:00 2001 From: Felix Dietze Date: Sat, 24 Aug 2024 17:57:37 +0200 Subject: [PATCH 2/3] fix warning --- jsdomClient/src/main/scala/HttpRpcTransport.scala | 1 - 1 file changed, 1 deletion(-) diff --git a/jsdomClient/src/main/scala/HttpRpcTransport.scala b/jsdomClient/src/main/scala/HttpRpcTransport.scala index 1b793347..f7890a6c 100644 --- a/jsdomClient/src/main/scala/HttpRpcTransport.scala +++ b/jsdomClient/src/main/scala/HttpRpcTransport.scala @@ -5,7 +5,6 @@ import cats.implicits._ import sloth.{Request, RequestTransport} import org.scalajs.dom -import scala.scalajs.js import scala.scalajs.js.JSConverters._ case class HttpRequestConfig( From 50797018bff169055b84eafbb9b3f9b7dd3c74d8 Mon Sep 17 00:00:00 2001 From: Felix Dietze Date: Mon, 26 Aug 2024 23:23:41 +0200 Subject: [PATCH 3/3] Update jsdomClient/src/main/scala/HttpRpcTransport.scala --- jsdomClient/src/main/scala/HttpRpcTransport.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsdomClient/src/main/scala/HttpRpcTransport.scala b/jsdomClient/src/main/scala/HttpRpcTransport.scala index f7890a6c..cd55fdcb 100644 --- a/jsdomClient/src/main/scala/HttpRpcTransport.scala +++ b/jsdomClient/src/main/scala/HttpRpcTransport.scala @@ -21,7 +21,7 @@ object HttpRpcTransport { url = s"${config.baseUri}/${request.method.traitName}/${request.method.methodName}" requestArgs = new dom.RequestInit { headers = config.headers.toJSDictionary; method = dom.HttpMethod.POST; body = request.payload } response <- Async[F].fromPromise(Async[F].delay(dom.fetch(url, requestArgs))) - _ <- Async[F].raiseWhen(!response.ok)(new Exception(s"HTTP error ${response.status}")) + _ <- Async[F].raiseWhen(!response.ok)(new Exception(s"${request.method.traitName}.${request.method.methodName} returned HTTP ${response.status}")) result <- Async[F].fromPromise(Async[F].delay(response.text())) } yield result }