I am trying to create a connection with Option[Tuple]and return the result in a disjunction, but my code looks a bit strange:
def ssh(config: GushConfig): \/[Throwable, Client] = {
val params = for {
host <- config.mysqlHost
port <- config.mysqlPort
user <- config.mysqlUser
password <- config.mysqlPassword
sshAddress <- config.sshTunnelAddress
sshTunnelUser <- config.sshTunnelUser
} yield (host, port, user, password, sshAddress, sshTunnelUser)
params match {
case Some((host, port, user, password, sshAddress, sshTunnelUser)) ⇒
Try({
// Do stuff that can fail and throw exceptions
new Client("127.0.0.1", lport, user, password)
}) match {
case Success(v) ⇒ v.right
case Failure(t) ⇒ t.left
}
case None ⇒
new Exception("Not enough parameters to initialize a ssh client").left
}
}
I first need to match my first Optionpattern to verify that I have all the necessary parameters, and then if I do, try connecting inside Try
and then converting the result of the attempt into a disjunction.
Is there a better way to do this conversion?
simao source
share