If you are writing a function that implies and I/O, and can fail, os highly recomended to encapsulate it in a Either or an Option.
1. Either approach
try{
val pingCmd:Publisher[bson.Document] =
mongoCollFactory.db.runCommand(BsonDocument.parse("""{"ping": 1}"""))
Rigth(pingCmd)
}catch{
case e:Exception => Left(e)
}
In another point of the execution, you can handle the exception or log a warning. You can find more info about either in ScalaDoc (https://www.scala-lang.org/api/2.9.3/scala/Either.html)
2. Option approach
Using the monad option, you can have a value in case the operation succeeds, or none value if it fails. This solution provide less information about the error, becouse None does not encapsulate information about the exception, while Either does it.
try{
val pingCmd:Publisher[bson.Document] =
mongoCollFactory.db.runCommand(BsonDocument.parse("""{"ping": 1}"""))
Rigth(pingCmd)
}.toOption