4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Event;
14 use Symfony\Component\Console\Command\Command;
15 use Symfony\Component\Console\Exception\InvalidArgumentException;
16 use Symfony\Component\Console\Input\InputInterface;
17 use Symfony\Component\Console\Output\OutputInterface;
20 * Allows to handle throwables thrown while running a command.
22 * @author Wouter de Jong <wouter@wouterj.nl>
24 final class ConsoleErrorEvent extends ConsoleEvent
29 public function __construct(InputInterface $input, OutputInterface $output, $error, Command $command = null)
31 parent::__construct($command, $input, $output);
33 $this->setError($error);
37 * Returns the thrown error/exception.
41 public function getError()
47 * Replaces the thrown error/exception.
49 * @param \Throwable $error
51 public function setError($error)
53 if (!$error instanceof \Throwable && !$error instanceof \Exception) {
54 throw new InvalidArgumentException(sprintf('The error passed to ConsoleErrorEvent must be an instance of \Throwable or \Exception, "%s" was passed instead.', is_object($error) ? get_class($error) : gettype($error)));
57 $this->error = $error;
63 * @param int $exitCode The command exit code
65 public function setExitCode($exitCode)
67 $this->exitCode = (int) $exitCode;
69 $r = new \ReflectionProperty($this->error, 'code');
70 $r->setAccessible(true);
71 $r->setValue($this->error, $this->exitCode);
77 * @return int The command exit code
79 public function getExitCode()
81 return null !== $this->exitCode ? $this->exitCode : (is_int($this->error->getCode()) && 0 !== $this->error->getCode() ? $this->error->getCode() : 1);