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 $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(); $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 $error = $e->getMessage();
{ throw new SshCommandException($error);
return $this->DoCommand($command); }
} catch (SshConnectException $e)
catch (SshCommandException $e) {
{ $error = $e->getMessage();
$error = $e->getMessage(); throw new SshCommandException("Could not run command {$command}: Failed to connect: {$error}");
throw new SshCommandException($error); }
} catch (SshExitException $e)
{
$error = $e->getMessage();
throw new SshExitException($error);
} }
} }
@ -99,7 +93,7 @@ class SshConnector extends CPHPBaseClass
return false; return false;
} }
private function DoCommand($command) private function DoCommand($command, $throw_exception)
{ {
$command = str_replace("'", "\'", $command); $command = str_replace("'", "\'", $command);
$command = "{$this->helper} '{$command}'"; $command = "{$this->helper} '{$command}'";

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

Loading…
Cancel
Save