Skip to content

Commit

Permalink
chore(http): better WarpScript error extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
miton18 committed Jan 13, 2025
1 parent 5e3cd52 commit 2a659c7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
39 changes: 26 additions & 13 deletions src/main/scala/com/clevercloud/warp10client/Runner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import io.circe.*
import io.circe.parser.*
import com.clevercloud.warp10client.models.gts_module.*

import scala.concurrent.Future

object Runner {
type WarpScript = String

Expand Down Expand Up @@ -48,20 +50,31 @@ object Runner {
import warpClientContext._

Flow[Try[HttpResponse]].flatMapConcat {
case Success(httpResponse) => {
if (httpResponse.status == StatusCodes.OK) {
Source.future(
WarpClientUtils.readAllDataBytes(httpResponse.entity.dataBytes)
)
case Success(httpResponse) =>
Source.future(if (httpResponse.status == StatusCodes.OK) {
WarpClientUtils.readAllDataBytes(httpResponse.entity.dataBytes)
} else if (httpResponse.status == StatusCodes.InternalServerError) {
// In case of 500, we try to read headers which are human-readable and fallback on response body which is HTML
val error: Option[String] = httpResponse.headers.findLast(_.is("x-warp10-error-message")).map(_.value)
val line: Option[Int] = httpResponse.headers.findLast(_.is("x-warp10-error-line")).map(_.value).flatMap { v =>
try {
Some(v.toInt)
} catch {
case _: Exception => None
}
}

error match
case Some(err) => Future.successful(throw WarpException(err, line))
case None =>
WarpClientUtils.readAllDataBytes(httpResponse.entity.dataBytes).map(WarpException(_, line)).map(throw _)
} else {
Source.future(
WarpClientUtils
.readAllDataBytes(httpResponse.entity.dataBytes)
.map(content => WarpException(s"HTTP status: ${httpResponse.status.intValue.toString}: $content"))
.map(throw _)
)
}
}
WarpClientUtils
.readAllDataBytes(httpResponse.entity.dataBytes)
.map(content => WarpException(s"HTTP status: ${httpResponse.status.intValue.toString}: $content"))
.map(throw _)

})
case Failure(e) => throw e
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ case class WarpClientContext(
implicit def implicitWarpConfiguration: WarpConfiguration = configuration
}

case class WarpException(error: String) extends Exception(error)
case class WarpException(error: String, line: Option[Int] = None) extends Exception(error)

object `X-Warp10-Token` {

Expand Down

0 comments on commit 2a659c7

Please sign in to comment.