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 :
- why service being called part of initialising routes?
- 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
Post a Comment