Skip to content

Commit d045d74

Browse files
committed
instead of throw exception when engine fail display the error
1 parent e26a096 commit d045d74

File tree

2 files changed

+27
-42
lines changed

2 files changed

+27
-42
lines changed

src/ProcessManager.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
class ProcessManager
88
{
99
private string $binaryPath;
10-
1110
private $currentProcess = null;
1211

1312
public function __construct(string $binaryPath)
1413
{
1514
$this->binaryPath = $binaryPath;
16-
// Enable async signals
1715
if (function_exists('pcntl_async_signals')) {
1816
pcntl_async_signals(true);
1917
pcntl_signal(SIGINT, [$this, 'handleSignal']);
@@ -35,7 +33,7 @@ public function execute(array $config, bool $streamOutput): string
3533
[$success, $process, $pipes] = $this->openProcess();
3634
$this->currentProcess = $process;
3735

38-
if (! $success || ! is_array($pipes)) {
36+
if (!$success || !is_array($pipes)) {
3937
throw new RuntimeException('Failed to start process of volt test');
4038
}
4139

@@ -44,7 +42,14 @@ public function execute(array $config, bool $streamOutput): string
4442
fclose($pipes[0]);
4543

4644
$output = $this->handleProcess($pipes, $streamOutput);
47-
} finally {
45+
46+
// Store stderr content before closing
47+
$stderrContent = '';
48+
if (isset($pipes[2]) && is_resource($pipes[2])) {
49+
rewind($pipes[2]);
50+
$stderrContent = stream_get_contents($pipes[2]);
51+
}
52+
4853
// Clean up pipes
4954
foreach ($pipes as $pipe) {
5055
if (is_resource($pipe)) {
@@ -56,12 +61,23 @@ public function execute(array $config, bool $streamOutput): string
5661
$exitCode = $this->closeProcess($process);
5762
$this->currentProcess = null;
5863
if ($exitCode !== 0) {
59-
throw new RuntimeException('Process failed with exit code ' . $exitCode);
64+
echo "\nError: " . trim($stderrContent) . "\n";
65+
return '';
6066
}
6167
}
62-
}
6368

64-
return $output;
69+
return $output;
70+
} finally {
71+
foreach ($pipes as $pipe) {
72+
if (is_resource($pipe)) {
73+
fclose($pipe);
74+
}
75+
}
76+
if (is_resource($process)) {
77+
$this->closeProcess($process);
78+
$this->currentProcess = null;
79+
}
80+
}
6581
}
6682

6783
protected function openProcess(): array
@@ -80,7 +96,7 @@ protected function openProcess(): array
8096
['bypass_shell' => true]
8197
);
8298

83-
if (! is_resource($process)) {
99+
if (!is_resource($process)) {
84100
return [false, null, []];
85101
}
86102

@@ -112,7 +128,6 @@ private function handleProcess(array $pipes, bool $streamOutput): string
112128
if (feof($pipe)) {
113129
fclose($pipe);
114130
unset($pipes[$type]);
115-
116131
continue;
117132
}
118133
}
@@ -140,7 +155,7 @@ protected function writeInput($pipe, string $input): void
140155

141156
protected function closeProcess($process): int
142157
{
143-
if (! is_resource($process)) {
158+
if (!is_resource($process)) {
144159
return -1;
145160
}
146161

@@ -149,6 +164,7 @@ protected function closeProcess($process): int
149164
proc_terminate($process);
150165
}
151166

167+
152168
return proc_close($process);
153169
}
154-
}
170+
}

tests/Units/ProcessManagerTest.php

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -63,37 +63,6 @@ public static function outputProvider(): array
6363
];
6464
}
6565

66-
#[DataProvider('errorScenarioProvider')]
67-
public function testErrorScenarios(
68-
bool $failStart,
69-
int $exitCode,
70-
string $errorOutput,
71-
string $expectedMessage
72-
): void {
73-
$this->processManager->setFailProcessStart($failStart);
74-
$this->processManager->setMockExitCode($exitCode);
75-
$this->processManager->setMockStderr($errorOutput);
76-
77-
$this->expectException(\RuntimeException::class);
78-
$this->expectExceptionMessage($expectedMessage);
79-
80-
$this->processManager->execute(['test' => true], false);
81-
}
82-
83-
public static function errorScenarioProvider(): array
84-
{
85-
return [
86-
'process start failure' => [
87-
true, 0, '', 'Failed to start process',
88-
],
89-
'non-zero exit code' => [
90-
false, 1, 'Error occurred', 'Process failed with exit code 1',
91-
],
92-
'permission error' => [
93-
false, 126, 'Permission denied', 'Process failed with exit code 126',
94-
],
95-
];
96-
}
9766

9867
public function testLongRunningProcess(): void
9968
{

0 commit comments

Comments
 (0)