scala - Handle services returning Try values in Spray -


i working on codebase calling spray api need synchronously call service returns try spray need format , return on http.

my initial attempt looked :

// assume myservice has run method returns try[unit] lazy val myservice = new service()   val routes =    path("api") {     {       trytocomplete {         myservice.run()       }      }   } ~    path("api" / longnumber) { (num: long) =>     {       trytocomplete {         myservice.run(num)       }      }   }  def trytocomplete(result: => try[unit]): routing.route = result match {   case failure(t) => complete(statuscodes.badrequest, t.getmessage)   case success(_) => complete("success") } 

however caused myservice.run() called when application started. not sure why method called there no http call made.

so have 2 questions :

  1. why service being called part of initialising routes?
  2. what cleanest way handle case? imagine there few other end points following similar pattern. need able handle consistently.

even though have result parameter call-by-name, it'll evaluated you're doing

result match { 

for not evaluated, has within complete, ie code should (haven't tried compile this):

def trytocomplete(result: => try[unit]): routing.route = complete { result match {   case failure(t) => statuscodes.badrequest, t.getmessage   case success(_) => "success" } } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -