Skip to content

Commit

Permalink
Add http connect error and include in on server retriees
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Dumas committed Oct 8, 2024
1 parent a2ea991 commit 9f2965c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import ch.epfl.bluebrain.nexus.delta.sdk.http.HttpClientError._
import ch.epfl.bluebrain.nexus.delta.sdk.syntax._
import io.circe.{Decoder, Json}

import java.net.UnknownHostException
import java.net.{ConnectException, UnknownHostException}
import scala.concurrent.{ExecutionContext, TimeoutException}
import scala.reflect.ClassTag

Expand Down Expand Up @@ -116,6 +116,7 @@ object HttpClient {

@SuppressWarnings(Array("IsInstanceOf"))
private def toHttpError(req: HttpRequest): Throwable => HttpClientError = {
case e: ConnectException => HttpConnectError(req, e.getMessage)
case e: TimeoutException => HttpTimeoutError(req, e.getMessage)
case e: StreamTcpException if e.getCause.isInstanceOf[UnknownHostException] => HttpUnknownHost(req)
case e: Throwable => HttpUnexpectedError(req, e.getMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ object HttpClientError {
override val errorCode: Option[StatusCode] = None
}

/**
* A connection error (ex: connection refused).
*/
final case class HttpConnectError(req: HttpRequest, message: String) extends HttpClientError {
override val reason: String =
s"an HTTP response to endpoint '${req.uri}' with method '${req.method}' resulted in a connect error"
override val details: Option[String] = Some(s"the request failed due to '$message'")
override val errorCode: Option[StatusCode] = None
}

/**
* A timeout error.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.bluebrain.nexus.delta.sdk.http

import akka.http.scaladsl.model.StatusCodes.GatewayTimeout
import ch.epfl.bluebrain.nexus.delta.sdk.http.HttpClientError.{HttpServerStatusError, HttpTimeoutError, HttpUnexpectedError}
import ch.epfl.bluebrain.nexus.delta.sdk.http.HttpClientError.{HttpConnectError, HttpServerStatusError, HttpTimeoutError, HttpUnexpectedError}

trait HttpClientWorthRetry extends (HttpClientError => Boolean)

Expand All @@ -22,6 +22,7 @@ object HttpClientWorthRetry {
*/
val onServerError: HttpClientWorthRetry = {
case HttpServerStatusError(_, code, _) if code != GatewayTimeout => true
case _: HttpConnectError => true
case _: HttpTimeoutError => true
case err: HttpUnexpectedError if !err.message.contains("Connection refused") => true
case _ => false
Expand Down

0 comments on commit 9f2965c

Please sign in to comment.