8000 New ProcessStartFailedException · symfony/symfony@833bf1d · GitHub
[go: up one dir, main page]

Skip to content

Commit 833bf1d

Browse files
committed
New ProcessStartFailedException
1 parent 22d93a3 commit 833bf1d

File tree

7 files changed

+64
-19
lines changed

7 files changed

+64
-19
lines changed

src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ private static function startVulcain(HttpClientInterface $client)
331331

332332
try {
333333
$process->start();
334-
} catch (RuntimeException $e) {
334+
} catch (ProcessFailedException $e) {
335335
self::markTestSkipped('vulcain failed: '.$e->getMessage());
336336
}
337337

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 for processes failed during startup.
18+
*/
19+
class ProcessStartFailedException extends ProcessFailedException
20+
{
21+
private Process $process;
22+
23+
public function __construct(Process $process, ?string $message)
24+
{
25+
if ($process->isStarted()) {
26+
throw new InvalidArgumentException('Expected a process that failed during startup, but the given process was started successfully.');
27+
}
28+
29+
$error = sprintf('The command "%s" failed.'."\n\nWorking directory: %s\n\nError: %s",
30+
$process->getCommandLine(),
31+
$process->getWorkingDirectory(),
32+
$message ?? 'unknown'
33+
);
34+
35+
// Skip parent constructor
36+
RuntimeException::__construct($error);
37+
38+
$this->process = $process;
39+
}
40+
41+
public function getProcess(): Process
42+
{
43+
return $this->process;
44+
}
45+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
final class RunProcessFailedException extends RuntimeException
2020
{
21-
public function __construct(RuntimeException $exception, public readonly RunProcessContext $context)
21+
public function __construct(ProcessFailedException $exception, public readonly RunProcessContext $context)
2222
{
2323
parent::__construct($exception->getMessage(), $exception->getCode());
2424
}

src/Symfony/Component/Process/Messenger/RunProcessContext.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(
2727
Process $process,
2828
) {
2929
$this->exitCode = $process->getExitCode();
30-
$this->output = $process->isOutputDisabled() ? null : $process->getOutput();
31-
$this->errorOutput = $process->isOutputDisabled() ? null : $process->getErrorOutput();
30+
$this->output = !$process->isStarted() || $process->isOutputDisabled() ? null : $process->getOutput();
31+
$this->errorOutput = !$process->isStarted() || $process->isOutputDisabled() ? null : $process->getErrorOutput();
3232
}
3333
}

src/Symfony/Component/Process/Messenger/RunProcessMessageHandler.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public function __invoke(RunProcessMessage $message): RunProcessContext
2929
return new RunProcessContext($message, $process->mustRun());
3030
} catch (ProcessFailedException $e) {
3131
throw new RunProcessFailedException($e, new RunProcessContext($message, $e->getProcess()));
32-
} catch (RuntimeException< 1E0A /span> $e) {
33-
throw new RunProcessFailedException($e, new RunProcessContext($message, $process->disableOutput()));
3432
}
3533
}
3634
}

src/Symfony/Component/Process/Process.php

Lines changed: 13 additions & 12 deletions
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\ProcessStartFailedException;
1718
use Symfony\Component\Process\Exception\ProcessSignaledException;
1819
use Symfony\Component\Process\Exception\ProcessTimedOutException;
1920
use Symfony\Component\Process\Exception\RuntimeException;
@@ -233,11 +234,11 @@ public function __clone()
233234
*
234235
* @return int The exit status code
235236
*
236-
* @throws RuntimeException When process can't be launched
237-
* @throws RuntimeException When process is already running
238-
* @throws ProcessTimedOutException When process timed out
239-
* @throws ProcessSignaledException When process stopped after receiving signal
240-
* @throws LogicException In case a callback is provided and output has been disabled
237+
* @throws ProcessStartFailedException When process can't be launched
238+
* @throws RuntimeException When process is already running
239+
* @throws ProcessTimedOutException When process timed out
240+
* @throws ProcessSignaledException When process stopped after receiving signal
241+
* @throws LogicException In case a callback is provided and output has been disabled
241242
*
242243
* @final
243244
*/
@@ -284,9 +285,9 @@ public function mustRun(callable $callback = null, array $env = []): static
284285
* @param callable|null $callback A PHP callback to run whenever there is some
285286
* output available on STDOUT or STDERR
286287
*
287-
* @throws RuntimeException When process can't be launched
288-
* @throws RuntimeException When process is already running
289-
* @throws LogicException In case a callback is provided and output has been disabled
288+
* @throws ProcessStartFailedException When process can't be launched
289+
* @throws RuntimeException When process is already running
290+
* @throws LogicException In case a callback is provided and output has been disabled
290291
*/
291292
public function start(callable $callback = null, array $env = []): void
292293
{
@@ -338,7 +339,7 @@ public function start(callable $callback = null, array $env = []): void
338339
throw new RuntimeException(sprintf('The provided cwd "%s" does not exist.', $this->cwd));
339340
}
340341

341-
$lastError = 'unknown reason';
342+
$lastError = null;
342343
set_error_handler(function ($type, $msg) use (&$lastError) {
343344
$lastError = $msg;
344345

@@ -351,7 +352,7 @@ public function start(callable $callback = null, array $env = []): void
351352
}
352353

353354
if (!\is_resource($process)) {
354-
throw new RuntimeException('Unable to launch a new process: '.$lastError);
355+
throw new ProcessStartFailedException($this, $lastError);
355356
}
356357
$this->process = $process;
357358
$this->status = self::STATUS_STARTED;
@@ -376,8 +377,8 @@ public function start(callable $callback = null, array $env = []): void
376377
* @param callable|null $callback A PHP callback to run whenever there is some
377378
* output available on STDOUT or STDERR
378379
*
379-
* @throws RuntimeException When process can't be launched
380-
* @throws RuntimeException When process is already running
380+
* @throws ProcessStartFailedException When process can't be launched
381+
* @throws RuntimeException When process is already running
381382
*
382383
* @see start()
383384
*

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Process\Exception\LogicException;
1717
use Symfony\Component\Process\Exception\ProcessFailedException;
1818
use Symfony\Component\Process\Exception\ProcessSignaledException;
19+
use Symfony\Component\Process\Exception\ProcessStartFailedException;
1920
use Symfony\Component\Process\Exception\ProcessTimedOutException;
2021
use Symfony\Component\Process\Exception\RuntimeException;
2122
use Symfony\Component\Process\InputStream;
@@ -73,7 +74,7 @@ public function testInvalidCommand(Process $process)
7374
{
7475
try {
7576
$this->assertSame('\\' === \DIRECTORY_SEPARATOR ? 1 : 127, $process->run());
76-
} catch (RuntimeException $e) {
77+
} catch (ProcessStartFailedException $e) {
7778
// An invalid command might already fail during start since PHP 8.3 for platforms
7879
// supporting posix_spawn(), see https://github.com/php/php-src/issues/12589
7980
$this->assertStringContainsString('No such file or directory', $e->getMessage());

0 commit comments

Comments
 (0)
0