8000 Introduce signaled process specific exception class · symfony/symfony@2b2cc07 · GitHub
[go: up one dir, main page]

Skip to content

File tree

5 files changed

+83
-3
lines changed

5 files changed

+83
-3
lines changed

src/Symfony/Component/Process/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* added the `Process::isTtySupported()` method that allows to check for TTY support
88
* made `PhpExecutableFinder` look for the `PHP_BINARY` env var when searching the php binary
9+
* added the `ProcessSignaledException` class to properly catch signaled process errors
910

1011
4.0.0
1112
-----
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Process\Exception;
13+
14+
use Symfony\Component\Process\Process;
15+
16+
/**
17+
* @author Sullivan Senechal <soullivaneuh@gmail.com>
18+
*/
19+
class ProcessRuntimeException extends RuntimeException
20+
{
21+
private $process;
22+
23+
public function __construct(Process $process, string $message = '')
24+
{
25+
parent::__construct($message);
26+
27+
$this->process = $process;
28+
}
29+
30+
public function getProcess(): Process
31+
{
32+
return $this->process;
33+
}
34+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Process\Exception;
13+
14+
use Symfony\Component\Process\Process;
15+
16+
/**
17+
* Exception that is thrown when a process has been signaled.
18+
*
19+
* @author Sullivan Senechal <soullivaneuh@gmail.com>
20+
*/
21+
final class ProcessSignaledException extends RuntimeException
22+
{
23+
private $process;
24+
25+
public function __construct(Process $process)
26+
{
27+
$this->process = $process;
28+
29+
parent::__construct(sprintf(
30+
'The process has been signaled with signal "%s".',
31+
$process->getTermSignal()
32+
));
33+
}
34+
35+
public function getProcess()
36+
{
37+
return $this->process;
38+
}
39+
40+
public function getSignal(): int
41+
{
42+
return $this->getProcess()->getTermSignal();
43+
}
44+
}

src/Symfony/Component/Process/Process.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Process\Exception\InvalidArgumentException;
1515
use Symfony\Component\Process\Exception\LogicException;
1616
use Symfony\Component\Process\Exception\ProcessFailedException;
17+
use Symfony\Component\Process\Exception\ProcessSignaledException;
1718
use Symfony\Component\Process\Exception\ProcessTimedOutException;
1819
use Symfony\Component\Process\Exception\RuntimeException;
1920
use Symfony\Component\Process\Pipes\PipesInterface;
@@ -387,7 +388,7 @@ public function wait(callable $callback = null)
387388
}
388389

389390
if ($this->processInformation['signaled'] && $this->processInformation['termsig'] !== $this->latestSignal) {
390-
throw new RuntimeException(sprintf('The process has been signaled with signal "%s".', $this->processInformation['termsig']));
391+
throw new ProcessSignaledException($this);
391392
}
392393

393394
return $this->exitcode;

src/Symfony/Component/Process/Tests/ProcessTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,8 @@ public function testProcessIsSignaledIfStopped()
688688
}
689689

690690
/**
691-
* @expectedException \Symfony\Component\Process\Exception\RuntimeException
692-
* @expectedExceptionMessage The process has been signaled
691+
* @expectedException \Symfony\Component\Process\Exception\ProcessSignaledException
692+
* @expectedExceptionMessage The process has been signaled with signal "9".
693693
*/
694694
public function testProcessThrowsExceptionWhenExternallySignaled()
695695
{

0 commit comments

Comments
 (0)
0