Drastically improved try/catch structure

feature/node-rewrite
Sven Slootweg 12 years ago
parent 7a809b7199
commit 1b8a9f2d19

@ -28,37 +28,31 @@ class SshConnector extends CPHPBaseClass
public $helper = "~/runhelper";
public function RunCommand($command)
public function RunCommand($command, $throw_exception)
{
if($this->connected == false && $this->authenticated == false)
try
{
try
if($this->connected == false && $this->authenticated == false)
{
$this->Connect();
return $this->DoCommand($command);
}
catch (SshConnectException $e)
{
$error = $e->getMessage();
throw new SshCommandException("Could not run command {$command}: Failed to connect: {$error}");
}
catch (SshCommandException $e)
{
$error = $e->getMessage();
throw new SshCommandException($error);
}
return $this->DoCommand($command, $throw_exception);
}
else
catch (SshCommandException $e)
{
try
{
return $this->DoCommand($command);
}
catch (SshCommandException $e)
{
$error = $e->getMessage();
throw new SshCommandException($error);
}
$error = $e->getMessage();
throw new SshCommandException($error);
}
catch (SshConnectException $e)
{
$error = $e->getMessage();
throw new SshCommandException("Could not run command {$command}: Failed to connect: {$error}");
}
catch (SshExitException $e)
{
$error = $e->getMessage();
throw new SshExitException($error);
}
}
@ -99,7 +93,7 @@ class SshConnector extends CPHPBaseClass
return false;
}
private function DoCommand($command)
private function DoCommand($command, $throw_exception)
{
$command = str_replace("'", "\'", $command);
$command = "{$this->helper} '{$command}'";

@ -7,5 +7,5 @@ $ssh->host = "cvm-vz.cryto.net";
$ssh->key = "/etc/cvm/key";
$ssh->pubkey = "/etc/cvm/key.pub";
var_dump($ssh->RunCommand("cp derp2 derp"));
var_dump($ssh->RunCommand("df -h", true));
?>

Loading…
Cancel
Save