Complete generate a null value in akka-http

20 views
Skip to first unread message

Samuel Heredia

unread,
Oct 20, 2017, 4:15:46 PM10/20/17
to Akka User List
Hi! , I have a Route where if a function finds a data in the DB responds a value of type Json,
when it does not find the value it responds with null and therefore the complete responds null.
I want to validate this so that it does not respond null, but a String that says: "Not found", this is my route, any help? Thanks

val route = pathPrefix("auth") {
path("signIn") {
pathEndOrSingleSlash {
post {
entity(as[LoginPassword]) { loginPassword =>
val a = signIn(loginPassword.login, loginPassword.password).map(_.asJson)
if(signIn(loginPassword.login, loginPassword.password).map(_.asJson) == null){
complete(states.map(_.asJson))
}else {
def getObject : Option[Any] = Option(signIn(loginPassword.login, loginPassword.password).map(_.asJson))
val ahh = signIn(loginPassword.login, loginPassword.password).map(_.asJson)
if(getObject.isEmpty || getObject == null){ ///////NOT FOUND
complete("Not Found")
}else {
complete(complete(signIn(loginPassword.login, loginPassword.password).map(_.asJson))
}
//complete(signIn(loginPassword.login, loginPassword.password).map(_.asJson))
}
}
}
}
}

Konrad “ktoso” Malawski

unread,
Oct 20, 2017, 4:19:28 PM10/20/17
to akka...@googlegroups.com, Samuel Heredia
Don’t return null in scala code, that’s very not-scala-style.
Instead you could return empty things, or best rather even to throw an exception and make your handler

-- 
Cheers,
Konrad 'ktoso' Malawski
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Samuel Heredia

unread,
Oct 25, 2017, 9:04:14 AM10/25/17
to Akka User List
Thanks!!
      I finally realized this
               

val route = pathPrefix("auth") {
path("signIn") {
    post {
entity(as[LoginPassword]) { loginPassword =>
        val token = signIn(loginPassword.login, loginPassword.password)
val response = token.map(_ match {
case Some(token) => prepareHttpResponse(StatusCodes.OK, token.asJson.toString)
case None => prepareHttpResponse(StatusCodes.Unauthorized, "{reason: \"not found\"")
})
complete(response)
}
}
}


Samuel Heredia

unread,
Oct 25, 2017, 9:04:59 AM10/25/17
to Akka User List
Reply all
Reply to author
Forward
0 new messages