Browse Source

Add ScriptExecutionException for CLI scripts and avoid showing the full exception to users, fixes #5281

Jordi Boggiano 8 years ago
parent
commit
aaf2c06e7b

+ 3 - 0
src/Composer/Console/Application.php

@@ -27,6 +27,7 @@ use Composer\IO\IOInterface;
 use Composer\IO\ConsoleIO;
 use Composer\Json\JsonValidationException;
 use Composer\Util\ErrorHandler;
+use Composer\EventDispatcher\ScriptExecutionException;
 
 /**
  * The console application that handles the commands
@@ -217,6 +218,8 @@ class Application extends BaseApplication
             }
 
             return $result;
+        } catch (ScriptExecutionException $e) {
+            return $e->getCode();
         } catch (\Exception $e) {
             $this->hintCommonErrors($e);
             throw $e;

+ 4 - 4
src/Composer/EventDispatcher/EventDispatcher.php

@@ -181,9 +181,9 @@ class EventDispatcher
                     }
                     $exec = $phpPath . '  ' . realpath($_SERVER['argv'][0]) . substr($callable, 9);
                     if (0 !== ($exitCode = $this->process->execute($exec))) {
-                        $this->io->writeError(sprintf('<error>Script %s handling the %s event returned with an error</error>', $callable, $event->getName()));
+                        $this->io->writeError(sprintf('<error>Script %s handling the %s event returned with error code '.$exitCode.'</error>', $callable, $event->getName()));
 
-                        throw new \RuntimeException('Error Output: '.$this->process->getErrorOutput(), $exitCode);
+                        throw new ScriptExecutionException('Error Output: '.$this->process->getErrorOutput(), $exitCode);
                     }
                 } else {
                     if (!$this->getListeners(new Event($scriptName))) {
@@ -221,9 +221,9 @@ class EventDispatcher
                     $this->io->writeError(sprintf('> %s', $exec));
                 }
                 if (0 !== ($exitCode = $this->process->execute($exec))) {
-                    $this->io->writeError(sprintf('<error>Script %s handling the %s event returned with an error</error>', $callable, $event->getName()));
+                    $this->io->writeError(sprintf('<error>Script %s handling the %s event returned with error code '.$exitCode.'</error>', $callable, $event->getName()));
 
-                    throw new \RuntimeException('Error Output: '.$this->process->getErrorOutput(), $exitCode);
+                    throw new ScriptExecutionException('Error Output: '.$this->process->getErrorOutput(), $exitCode);
                 }
             }
 

+ 20 - 0
src/Composer/EventDispatcher/ScriptExecutionException.php

@@ -0,0 +1,20 @@
+<?php
+
+/*
+ * This file is part of Composer.
+ *
+ * (c) Nils Adermann <naderman@naderman.de>
+ *     Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Composer\EventDispatcher;
+
+/**
+ * @author Jordi Boggiano <j.boggiano@seld.be>
+ */
+class ScriptExecutionException extends \RuntimeException
+{
+}