Skip to content

Commit e992977

Browse files
authored
Make TooManyRedirectsException part of the read exception hierarchy (#2396)
1 parent cb4124b commit e992977

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

core/src/main/scala/sttp/client4/SttpClientException.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sttp.client4
22

33
import sttp.monad.MonadError
4+
import sttp.model.Uri
45

56
/** Known exceptions that might occur when using a backend. Currently this covers:
67
* - connect exceptions: when a connection (tcp socket) can't be established to the target host
@@ -28,6 +29,9 @@ object SttpClientException extends SttpClientExceptionExtensions {
2829

2930
class TimeoutException(request: GenericRequest[_, _], cause: Exception) extends ReadException(request, cause)
3031

32+
class TooManyRedirectsException(request: GenericRequest[_, _], val redirects: Int)
33+
extends ReadException(request, null)
34+
3135
def adjustExceptions[F[_], T](
3236
monadError: MonadError[F]
3337
)(t: => F[T])(usingFn: Exception => Option[Exception]): F[T] =

core/src/main/scala/sttp/client4/wrappers/FollowRedirectsBackend.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ abstract class FollowRedirectsBackend[F[_], P] private (
3737
): F[Response[T]] =
3838
response.header(HeaderNames.Location).fold(monad.unit(response)) { loc =>
3939
if (redirects >= request.options.maxRedirects) {
40-
monad.error(TooManyRedirectsException(request.uri, redirects))
40+
monad.error(new SttpClientException.TooManyRedirectsException(request, redirects))
4141
} else {
4242
followRedirect(request, response, redirects, loc)
4343
}
@@ -121,8 +121,6 @@ object FollowRedirectsBackend {
121121
val DefaultUriTransform: Uri => Uri = (uri: Uri) => uri
122122
}
123123

124-
case class TooManyRedirectsException(uri: Uri, redirects: Int) extends Exception
125-
126124
/** @param transformUri
127125
* Defines if and how [[Uri]] s from the `Location` header should be transformed. For example, this enables changing
128126
* the encoding of host, path, query and fragment segments to be more strict or relaxed.

core/src/test/scalajvm/sttp/client4/testing/HttpTestExtensions.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import sttp.model.{Header, HeaderNames, StatusCode}
1010
import scala.concurrent.Future
1111
import HttpTest.endpoint
1212
import org.scalatest.freespec.AsyncFreeSpecLike
13-
import sttp.client4.wrappers.{DigestAuthenticationBackend, FollowRedirectsBackend, TooManyRedirectsException}
13+
import sttp.client4.wrappers.{DigestAuthenticationBackend, FollowRedirectsBackend}
1414
import sttp.model.headers.CookieWithMeta
1515
import sttp.model.Encodings
1616
import java.util.zip.GZIPInputStream
@@ -132,17 +132,17 @@ trait HttpTestExtensions[F[_]] extends AsyncFreeSpecLike { self: HttpTest[F] =>
132132
"break redirect loops" in {
133133
// sync backends can throw exceptions when evaluating send(), before the toFuture() conversion
134134
Future(loop.send(backend).toFuture()).flatMap(identity).failed.map {
135-
case TooManyRedirectsException(_, redirects) =>
136-
redirects shouldBe FollowRedirectsBackend.MaxRedirects
135+
case e: SttpClientException.TooManyRedirectsException =>
136+
e.redirects shouldBe FollowRedirectsBackend.MaxRedirects
137137
case e => fail(e)
138138
}
139139
}
140140

141141
"break redirect loops after user-specified count" in {
142142
val maxRedirects = 10
143143
Future(loop.maxRedirects(maxRedirects).send(backend).toFuture()).flatMap(identity).failed.collect {
144-
case TooManyRedirectsException(_, redirects) =>
145-
redirects shouldBe maxRedirects
144+
case e: SttpClientException.TooManyRedirectsException =>
145+
e.redirects shouldBe maxRedirects
146146
}
147147
}
148148

core/src/test/scalanative/sttp/client4/testing/SyncHttpTestExtensions.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package sttp.client4.testing
22

33
import org.scalatest.freespec.AnyFreeSpecLike
44
import sttp.client4._
5-
import sttp.client4.wrappers.{FollowRedirectsBackend, TooManyRedirectsException}
5+
import sttp.client4.wrappers.FollowRedirectsBackend
66
import sttp.model.{Header, StatusCode}
77

88
import java.io.File
@@ -38,14 +38,14 @@ trait SyncHttpTestExtensions extends AnyFreeSpecLike {
3838
}
3939

4040
"break redirect loops" in {
41-
intercept[TooManyRedirectsException] {
41+
intercept[SttpClientException.TooManyRedirectsException] {
4242
loop.send(backend)
4343
}.redirects shouldBe FollowRedirectsBackend.MaxRedirects
4444
}
4545

4646
"break redirect loops after user-specified count" in {
4747
val maxRedirects = 10
48-
intercept[TooManyRedirectsException] {
48+
intercept[SttpClientException.TooManyRedirectsException] {
4949
loop.maxRedirects(maxRedirects).send(backend)
5050
}.redirects shouldBe maxRedirects
5151
}

0 commit comments

Comments
 (0)