Skip to content

Commit

Permalink
Catch IllegalArgumentException for invalid URI from AHC (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmethvin authored Aug 31, 2017
1 parent ac202f4 commit 872c243
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ class AhcWSClientSpec(implicit val executionEnv: ExecutionEnv) extends Specifica
"url" should {
"throw an exception on invalid url" in {
withClient() { client =>
{ client.url("localhost") } must throwA[Throwable]
{ client.url("localhost") } must throwAn[IllegalArgumentException]
}
}

"not throw exception on valid url" in {
withClient() { client =>
{ client.url(s"http://localhost:$testServerPort") } must not(throwA[Throwable])
{ client.url(s"http://localhost:$testServerPort") } must not(throwAn[IllegalArgumentException])
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import play.shaded.ahc.org.asynchttpclient.{ Response => AHCResponse, _ }
import scala.collection.immutable.TreeMap
import scala.compat.java8.FunctionConverters
import scala.concurrent.{ Await, Future, Promise }
import scala.util.{ Failure, Success, Try }

/**
* A WS client backed by an AsyncHttpClient.
Expand Down Expand Up @@ -78,10 +77,14 @@ class StandaloneAhcWSClient @Inject() (asyncHttpClient: AsyncHttpClient)(implici

private def validate(url: String): Unit = {
// Recover from https://github.com/AsyncHttpClient/async-http-client/issues/1149
Try(Uri.create(url)).transform(Success(_), {
try {
Uri.create(url)
} catch {
case iae: IllegalArgumentException =>
throw new IllegalArgumentException(s"Invalid URL $url", iae)
case npe: NullPointerException =>
Failure(new IllegalArgumentException(s"Invalid URL $url", npe))
}).get
throw new IllegalArgumentException(s"Invalid URL $url", npe)
}
}

private[ahc] def executeStream(request: Request): Future[StreamedResponse] = {
Expand Down

0 comments on commit 872c243

Please sign in to comment.