scala.util.Try does not wrap Exception when used with Future and Play WS -
my understanding scala.util.try
wraps exceptions thrown code inside try { ... }
block.
here simple example wraps nullpointerexception.
object trydemo extends app { import scala.util.{failure, success, try} def dosomething(i: int): string = { if (i > 50) { println("going throw nullpointerexception @ index " + i) throw new nullpointerexception("- exception @ index " + i) } else "some-result" } val t: try[string] = try { var x = 0 while (x < 100) { dosomething(x) x += 1 } "result-" + x } val result: option[string] = t match { case success(s) => println("success " + s); some(s) case failure(f) => println("failure " + f.getmessage()); none } }
this gives following output excepted.
going throw nullpointerexception @ index 51
failure - exception @ index 51
however, when try following (with invalid url ensure fails every time)
import play.api.libs.ws._ val wsurl = "http://invalidurl:9000/something" val wsres: try[future[response]] = try { ws.url(wsurl).withrequesttimeout(200).withquerystring(("param", value)).get }
exceptions thrown instead of getting error wrapped inside try
. can please explain why works in first case , not in second ?
[info] java.net.connectexception: http://invalidurl:9000/something [info] @ com.ning.http.client.providers.netty.nettyconnectlistener.operationcomplete(nettyconnectlistener.java:103) [info] @ org.jboss.netty.channel.defaultchannelfuture.notifylistener(defaultchannelfuture.java:427) [info] @ org.jboss.netty.channel.defaultchannelfuture.addlistener(defaultchannelfuture.java:145) [info] @ com.ning.http.client.providers.netty.nettyasynchttpprovider.doconnect(nettyasynchttpprovider.java:1068) [info] @ com.ning.http.client.providers.netty.nettyasynchttpprovider.execute(nettyasynchttpprovider.java:890) [info] @ com.ning.http.client.asynchttpclient.executerequest(asynchttpclient.java:520) [info] @ play.api.libs.ws.ws$wsrequest.execute(ws.scala:177) [info] @ play.api.libs.ws.ws$wsrequestholder.get(ws.scala:386) [info] @ controllers.geojson.geojsontransfomer$$anonfun$9.apply(geojsontransformer.scala:155) [info] @ controllers.geojson.geojsontransfomer$$anonfun$9.apply(geojsontransformer.scala:155) [info] ... [info] cause: java.nio.channels.unresolvedaddressexception: [info] @ sun.nio.ch.net.checkaddress(net.java:127) [info] @ sun.nio.ch.socketchannelimpl.connect(socketchannelimpl.java:640) [info] @ org.jboss.netty.channel.socket.nio.nioclientsocketpipelinesink.connect(nioclientsocketpipelinesink.java:108) [info] @ org.jboss.netty.channel.socket.nio.nioclientsocketpipelinesink.eventsunk(nioclientsocketpipelinesink.java:70) [info] @ org.jboss.netty.handler.codec.oneone.onetooneencoder.handledownstream(onetooneencoder.java:54) [info] @ org.jboss.netty.handler.codec.http.httpclientcodec.handledownstream(httpclientcodec.java:97) [info] @ org.jboss.netty.handler.stream.chunkedwritehandler.handledownstream(chunkedwritehandler.java:109) [info] @ org.jboss.netty.channel.channels.connect(channels.java:634) [info] @ org.jboss.netty.channel.abstractchannel.connect(abstractchannel.java:207) [info] @ org.jboss.netty.bootstrap.clientbootstrap.connect(clientbootstrap.java:229)
Comments
Post a Comment