The code below uses Slick 3.1.x to read a row from a table, attempting to catch any SQL errors. UserDB
is the Slick representation of the table, and User
is the related object.
This code does not compile in the failure
statement with the following error:
type mismatch; found : Unit required: scala.concurrent.Future[Option[user.manage.User]]
How to fix this to catch the SQL error?
def read (sk: Int): Future[Option[User]] = {
val users = TableQuery[UserDB]
val action = users.filter(_.sk === sk).result
val future = db.run(action)
future.onSuccess {
case result =>
if (!result.isEmpty)
Some(result(0))
else
None
}
future.onFailure { // <-- compilation error
case e => println (e.getMessage)
None
}
}
onFailure
reigsters a callback and returnsUnit
. Therefore your entire method returnsUnit
. – rethabNone
(as opposed to returnSome
if the record is read correctly), what should be the approach? – ps0604onFailure
but on of the methods onFuture
that can handle an Exception and still return something. – rethab