Drastically improved try/catch structure

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

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