From 1b8a9f2d1951c1d4e68cd4ebc83b929443a86f83 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Sun, 29 Apr 2012 06:33:21 +0200 Subject: [PATCH] Drastically improved try/catch structure --- frontend/classes/class.sshconnector.php | 44 +++++++++++-------------- frontend/test.php | 2 +- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/frontend/classes/class.sshconnector.php b/frontend/classes/class.sshconnector.php index 2b1e2b5..bc72b48 100644 --- a/frontend/classes/class.sshconnector.php +++ b/frontend/classes/class.sshconnector.php @@ -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}'"; diff --git a/frontend/test.php b/frontend/test.php index 95cb143..dc4f048 100644 --- a/frontend/test.php +++ b/frontend/test.php @@ -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)); ?>