From 68adb3b90361c7613ea43d0155361d5f20d9d5de Mon Sep 17 00:00:00 2001 From: Sullivan SENECHAL Date: Fri, 12 Jan 2018 12:47:51 +0100 Subject: [PATCH] Introduce signaled process specific exception class --- src/Symfony/Component/Process/CHANGELOG.md | 1 + .../Exception/ProcessSignaledException.php | 41 +++++++++++++++++++ src/Symfony/Component/Process/Process.php | 3 +- .../Component/Process/Tests/ProcessTest.php | 4 +- 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/Symfony/Component/Process/Exception/ProcessSignaledException.php diff --git a/src/Symfony/Component/Process/CHANGELOG.md b/src/Symfony/Component/Process/CHANGELOG.md index 631fb66eac03..354db592a152 100644 --- a/src/Symfony/Component/Process/CHANGELOG.md +++ b/src/Symfony/Component/Process/CHANGELOG.md @@ -6,6 +6,7 @@ CHANGELOG * added the `Process::isTtySupported()` method that allows to check for TTY support * made `PhpExecutableFinder` look for the `PHP_BINARY` env var when searching the php binary + * added the `ProcessSignaledException` class to properly catch signaled process errors 4.0.0 ----- diff --git a/src/Symfony/Component/Process/Exception/ProcessSignaledException.php b/src/Symfony/Component/Process/Exception/ProcessSignaledException.php new file mode 100644 index 000000000000..d4d322756f39 --- /dev/null +++ b/src/Symfony/Component/Process/Exception/ProcessSignaledException.php @@ -0,0 +1,41 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Process\Exception; + +use Symfony\Component\Process\Process; + +/** + * Exception that is thrown when a process has been signaled. + * + * @author Sullivan Senechal + */ +final class ProcessSignaledException extends RuntimeException +{ + private $process; + + public function __construct(Process $process) + { + $this->process = $process; + + parent::__construct(sprintf('The process has been signaled with signal "%s".', $process->getTermSignal())); + } + + public function getProcess(): Process + { + return $this->process; + } + + public function getSignal(): int + { + return $this->getProcess()->getTermSignal(); + } +} diff --git a/src/Symfony/Component/Process/Process.php b/src/Symfony/Component/Process/Process.php index b3a5be205584..5ff683b3e138 100644 --- a/src/Symfony/Component/Process/Process.php +++ b/src/Symfony/Component/Process/Process.php @@ -14,6 +14,7 @@ use Symfony\Component\Process\Exception\InvalidArgumentException; use Symfony\Component\Process\Exception\LogicException; use Symfony\Component\Process\Exception\ProcessFailedException; +use Symfony\Component\Process\Exception\ProcessSignaledException; use Symfony\Component\Process\Exception\ProcessTimedOutException; use Symfony\Component\Process\Exception\RuntimeException; use Symfony\Component\Process\Pipes\PipesInterface; @@ -387,7 +388,7 @@ public function wait(callable $callback = null) } if ($this->processInformation['signaled'] && $this->processInformation['termsig'] !== $this->latestSignal) { - throw new RuntimeException(sprintf('The process has been signaled with signal "%s".', $this->processInformation['termsig'])); + throw new ProcessSignaledException($this); } return $this->exitcode; diff --git a/src/Symfony/Component/Process/Tests/ProcessTest.php b/src/Symfony/Component/Process/Tests/ProcessTest.php index 6b75b00a5493..9d36d247c5df 100644 --- a/src/Symfony/Component/Process/Tests/ProcessTest.php +++ b/src/Symfony/Component/Process/Tests/ProcessTest.php @@ -688,8 +688,8 @@ public function testProcessIsSignaledIfStopped() } /** - * @expectedException \Symfony\Component\Process\Exception\RuntimeException - * @expectedExceptionMessage The process has been signaled + * @expectedException \Symfony\Component\Process\Exception\ProcessSignaledException + * @expectedExceptionMessage The process has been signaled with signal "9". */ public function testProcessThrowsExceptionWhenExternallySignaled() {