Scala Strange Error with Partial Functions -
i have following piece of code fails compilation strange reason!
def createnewpowerplant = action.async(parse.tolerantjson) { request => request.body.validate[powerplantconfig].fold( errors => { future.successful( badrequest(json.obj("message" -> s"invalid powerplantconfig $errors")) ) }, success => { // fails here!! dbservice.newpowerplant(topowerplantrow(success)).recover { case nonfatal(ex) => future.successful { unprocessableentity( json.obj("message" -> s"could not create new powerplant because of ${ex.getmessage}") ) } } future.successful { ok("") } } ) }
and see reason:
controller.scala:103: type inferred `any`; may indicate programming error. [error] success => { [error] ^ [error] 1 error found [error] (compile:compileincremental) compilation failed [error] total time: 16 s, completed aug 21, 2017 9:46:30
any ideas why error happens? i'm sure has got compiler have similar code in controller compiles!
you have "-xfatal-warnings"
in build.sbt. makes compiler throw compilation error in regular case warning. check i've commented "-xfatal-warnings"
. sbt clean compile
gives this:
alex@positron /ssd2/projects/powerplantproblem/plant-simulator $ sbt clean compile [info] loading project definition /ssd2/projects/powerplantproblem/plant-simulator/project [info] updating {file:/ssd2/projects/powerplantproblem/plant-simulator/project/}plant-simulator-build... waiting lock on /home/alex/.ivy2/.sbt.ivy.lock available... [info] resolving org.fusesource.jansi#jansi;1.4 ... [info] done updating. [info] set current project plant-simulator (in build file:/ssd2/projects/powerplantproblem/plant-simulator/) [success] total time: 0 s, completed aug 21, 2017 3:39:57 pm [info] updating {file:/ssd2/projects/powerplantproblem/plant-simulator/}root... [info] resolving jline#jline;2.14.3 ... [info] done updating. [info] compiling 31 scala sources , 2 java sources /ssd2/projects/powerplantproblem/plant-simulator/target/scala-2.11/classes... [warn] /ssd2/projects/powerplantproblem/plant-simulator/app/com/inland24/plantsim/controllers/powerplantcontroller.scala:102: type inferred `any`; may indicate programming error. [warn] case some(row) => dbservice.newpowerplant(row) recoverwith{ [warn] ^ [warn] 1 warning found [success] total time: 23 s, completed aug 21, 2017 3:40:20 pm alex@positron /ssd2/projects/powerplantproblem/plant-simulator $
this means suggestion true. either disable "-xfatal-warnings"
or make code satisfy more strict requirements brings on.
now in order understand warning itself, take @ signature of recoverwith
method:
def recoverwith[u >: t](pf: partialfunction[throwable, future[u]])(implicit executor: executioncontext): future[u]
in case t
int
. common ancestor of int
, future[result]
any
of course. can make compilable "-xfatal-warnings"
inserting int
value here:
case some(row) => dbservice.newpowerplant(row) recoverwith{ case ex: exception => future.successful{ unprocessableentity( json.obj("message" -> s"could not create new powerplant because of ${ex.getmessage}") ) 5 } }
the warning makes sense here because code not clean: trying recover problem inserting row db (e.g. db server down) returning unprocessableentity
http status not make sense, taking account override intention after future.successful { ok("") }
.
hope helps.
Comments
Post a Comment