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

Skip to content

Commit 21f8ac0

Browse files
committed
Introduce signaled process specific exception class
1 parent 1df45e4 commit 21f8ac0

File tree

7 files changed

+83
-26
lines changed

7 files changed

+83
-26
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
-----

src/Symfony/Component/Process/Exception/ProcessFailedException.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
*
1919
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
2020
*/
21-
class ProcessFailedException extends RuntimeException
21+
class ProcessFailedException extends ProcessRuntimeException
2222
{
23-
private $process;
24-
2523
public function __construct(Process $process)
2624
{
2725
if ($process->isSuccessful()) {
@@ -42,13 +40,6 @@ public function __construct(Process $process)
4240
);
4341
}
4442

45-
parent::__construct($error);
46-
47-
$this->process = $process;
48-
}
49-
50-
public function getProcess()
51-
{
52-
return $this->process;
43+
parent::__construct($process, $error);
5344
}
5445
}
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 ProcessRuntimeException
22+
{
23+
public function __construct(Process $process)
24+
{
25+
parent::__construct($process, sprintf(
26+
'The process has been signaled with signal "%s".',
27+
$process->getTermSignal()
28+
));
29+
}
30+
31+
public function getSignal(): int
32+
{
33+
return $this->getProcess()->getTermSignal();
34+
}
35+
}

src/Symfony/Component/Process/Exception/ProcessTimedOutException.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,24 @@
1818
*
1919
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
2020
*/
21-
class ProcessTimedOutException extends RuntimeException
21+
class ProcessTimedOutException extends ProcessRuntimeException
2222
{
2323
const TYPE_GENERAL = 1;
2424
const TYPE_IDLE = 2;
2525

26-
private $process;
2726
private $timeoutType;
2827

2928
public function __construct(Process $process, int $timeoutType)
3029
{
31-
$this->process = $process;
3230
$this->timeoutType = $timeoutType;
3331

34-
parent::__construct(sprintf(
32+
parent::__construct($process);
33+
34+
$this->message = sprintf(
3535
'The process "%s" exceeded the timeout of %s seconds.',
3636
$process->getCommandLine(),
3737
$this->getExceededTimeout()
38-
));
39-
}
40-
41-
public function getProcess()
42-
{
43-
return $this->process;
38+
);
4439
}
4540

4641
public function isGeneralTimeout()
@@ -57,10 +52,10 @@ public function getExceededTimeout()
5752
{
5853
switch ($this->timeoutType) {
5954
case self::TYPE_GENERAL:
60-
return $this->process->getTimeout();
55+
return $this->getProcess()->getTimeout();
6156

6257
case self::TYPE_IDLE:
63-
return $this->process->getIdleTimeout();
58+
return $this->getProcess()->getIdleTimeout();
6459

6560
default:
6661
throw new \LogicException(sprintf('Unknown timeout type "%d".', $this->timeoutType));

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