Introduce signaled process specific exception class

This commit is contained in:
Sullivan SENECHAL 2018-01-12 12:47:51 +01:00
parent 1df45e4356
commit 68adb3b903
No known key found for this signature in database
GPG Key ID: FD42297788CB7D07
4 changed files with 46 additions and 3 deletions

View File

@ -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
-----

View File

@ -0,0 +1,41 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <soullivaneuh@gmail.com>
*/
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();
}
}

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\Process;
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 @@ class Process implements \IteratorAggregate
}
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;

View File

@ -688,8 +688,8 @@ class ProcessTest extends TestCase
}
/**
* @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()
{