Skip to content

Commit 4daaace

Browse files
committed
#14 catch SSH error and target stdErr to display better error message
1 parent 5fa0871 commit 4daaace

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

src/Jumper/Communicator/Ssh.php

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Jumper\Communicator;
44

55
use Jumper\Communicator;
6+
use Jumper\Exception\CommunicatorException;
67
use Net_SSH2 as Ssh2Client;
78

89
/**
@@ -14,13 +15,19 @@
1415
*/
1516
class Ssh implements Communicator
1617
{
18+
/**
19+
* @var \Net_SSH2
20+
*/
1721
private $ssh;
1822

1923
/**
2024
* @var $authentication \Jumper\Communicator\Authentication
2125
*/
2226
private $authentication;
2327

28+
/**
29+
* @var array
30+
*/
2431
private $defaultOptions = array(
2532
'host' => '127.0.0.1',
2633
'port' => '22',
@@ -29,6 +36,9 @@ class Ssh implements Communicator
2936
'callbacks' => array()
3037
);
3138

39+
/**
40+
* @param array $options
41+
*/
3242
public function __construct(array $options = array())
3343
{
3444
$this->defaultOptions = array_replace_recursive($this->defaultOptions, $options);
@@ -39,34 +49,69 @@ public function __construct(array $options = array())
3949
);
4050
}
4151

52+
/**
53+
*
54+
*/
4255
public function __destruct()
4356
{
4457
$this->close();
4558
}
4659

60+
/**
61+
* @param Authentication $authentication
62+
*/
4763
public function setAuthentication(Authentication $authentication)
4864
{
4965
$this->authentication = $authentication;
5066
}
5167

52-
public function isConnected() {
68+
/**
69+
* @return bool
70+
*/
71+
public function isConnected()
72+
{
5373
return !is_null($this->ssh) && $this->ssh->isConnected();
5474
}
5575

76+
/**
77+
* @throws \Jumper\Exception\CommunicatorException
78+
*/
5679
public function connect()
5780
{
5881
$authentication = null;
5982
if (!is_null($this->authentication)) {
6083
$authentication = $this->authentication->getAuthentication($this->ssh);
6184
}
62-
return $this->ssh->login($this->authentication->getUser(), $authentication);
85+
if (!$this->ssh->login($this->authentication->getUser(), $authentication)) {
86+
throw new CommunicatorException($this->ssh->getLastError(), $this->ssh->getExitStatus());
87+
}
6388
}
6489

90+
/**
91+
* @param $command
92+
*
93+
* @throws \RuntimeException
94+
* @throws \Jumper\Exception\CommunicatorException
95+
* @return String
96+
*/
6597
public function run($command)
6698
{
67-
return $this->ssh->exec($command);
99+
$result = $this->ssh->exec($command);
100+
if ($result === false) {
101+
throw new CommunicatorException($this->ssh->getLastError(), $this->ssh->getExitStatus());
102+
}
103+
104+
$error = $this->ssh->getStdError();
105+
if (!empty($error)) {
106+
throw new \RuntimeException($this->ssh->getStdError(), $this->ssh->getExitStatus());
107+
}
108+
109+
return $result;
68110
}
69111

112+
/**
113+
*
114+
*/
70115
public function close()
71116
{
72117
$this->ssh->disconnect();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Jumper\Exception;
4+
5+
/**
6+
* Communicator exception
7+
*
8+
* @package Jumper\Exception
9+
* @author Thibaud Leprêtre
10+
* @license MIT
11+
*/
12+
class CommunicatorException extends \Exception {
13+
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* User: t5e
4+
* Date: 2/28/14
5+
* Time: 5:18 PM
6+
*/
7+
8+
namespace Jumper\Exception;
9+
10+
11+
class ExecutorException extends \Exception {
12+
13+
}

src/Jumper/Executor.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Jumper;
44

55
use Jeremeamia\SuperClosure\SerializableClosure;
6+
use Jumper\Exception\ExecutorException;
67
use TinyPHP\Minifier;
78

89
/**
@@ -70,12 +71,20 @@ public function run(\Closure $closure)
7071
);
7172

7273
return $this->stringifier->toObject($output);
74+
} catch (\RuntimeException $e) {
75+
throw new ExecutorException('An error occurs when execution php on target host with the following message: '
76+
. $e->getMessage());
7377
} catch (\Exception $e) {
7478
// todo Manage exception
7579
throw $e;
7680
}
7781
}
7882

83+
protected function isPhpError($string)
84+
{
85+
return strpos($string, 'PHP Notice:', 1);
86+
}
87+
7988
/**
8089
* Unserializer source code that is mandatory to unserialize object
8190
*

0 commit comments

Comments
 (0)