Improved detection of connection errors

This commit is contained in:
Robin Appelman 2015-12-31 20:04:07 +01:00
commit a07a5ad2ec
2 changed files with 21 additions and 3 deletions

View file

@ -39,13 +39,22 @@ class Connection extends RawConnection {
if (!$this->isValid()) {
throw new ConnectionException('Connection not valid');
}
$line = $this->readLine(); //first line is prompt
$this->checkConnectionError($line);
$promptLine = $this->readLine(); //first line is prompt
$this->checkConnectionError($promptLine);
$output = array();
$line = $this->readLine();
if ($line === false) {
throw new ConnectException('Unknown error');
if ($promptLine) { //maybe we have some error we missed on the previous line
throw new ConnectException('Unknown error (' . $promptLine . ')');
} else {
$error = $this->readError(); // maybe something on stderr
if ($error) {
throw new ConnectException('Unknown error (' . $error . ')');
} else {
throw new ConnectException('Unknown error');
}
}
}
$length = mb_strlen(self::DELIMITER);
while (mb_substr($line, 0, $length) !== self::DELIMITER) { //next prompt functions as delimiter

View file

@ -94,6 +94,15 @@ class RawConnection {
return stream_get_line($this->getOutputStream(), 4086, "\n");
}
/**
* read a line of output
*
* @return string
*/
public function readError() {
return trim(stream_get_line($this->getErrorStream(), 4086));
}
/**
* get all output until the process closes
*