From 7c9b4aca5e896fa69a1a80aa26b997de0906c7be Mon Sep 17 00:00:00 2001 From: Klaus Silveira Date: Thu, 31 May 2012 01:07:49 -0300 Subject: [PATCH] Better error handling in command execution --- lib/Git/Client.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/Git/Client.php b/lib/Git/Client.php index a3cc055..960f5d6 100644 --- a/lib/Git/Client.php +++ b/lib/Git/Client.php @@ -107,15 +107,25 @@ class Client */ public function run(Repository $repository, $command) { - $descriptors = array(0 => array("pipe", "r"), 1 => array("pipe", "w")); + $descriptors = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w")); $process = proc_open($this->getPath() . ' ' . $command, $descriptors, $pipes, $repository->getPath()); - if (is_resource($process)) { - $stdout = stream_get_contents($pipes[1]); - fclose($pipes[1]); - proc_close($process); - return $stdout; + if (!is_resource($process)) { + throw new \RuntimeException('Unable to execute command: ' . $command); } + + $stderr = stream_get_contents($pipes[2]); + fclose($pipes[2]); + + if (!empty($stderr)) { + throw new \RuntimeException($stderr); + } + + $stdout = stream_get_contents($pipes[1]); + fclose($pipes[1]); + + proc_close($process); + return $stdout; } /**