scala - Futures in For comprehension. Detect failure -


i'm using scala's comprehension wait until several futures executed. want handle onfailure (i want write error message log). how can achieve it?

this code:

val f1 = future {...} val f2 = future {...}  {    res1 <- f1   res2 <- f2 } {   // means both futures executed   process(res1, res2) }  

if want write error message log file can chain error logging onto onfailure part:

val f1 = future.successful("test") val f2 = future.failed(new exception("failed"))  def errorlogging(whichfuture: string): partialfunction[throwable, unit] = {   // here have option of matching on different exceptions , logging different things   case ex: exception =>     // more sophisticated logging :)     println(whichfuture +": "+ ex.getmessage) }  f1.onfailure(errorlogging("f1")) f2.onfailure(errorlogging("f2"))  val res = {   res1 <- f1   res2 <- f2 } yield {    // means both futures executed   println(res1 + res2) }  await.result(res, duration.inf) 

this print out:

exception in thread "main" java.lang.exception: failed    @ [...] f2: failed 

as see issue things might happen out of order , logging might far away when exception logged.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -