10000 Merge pull request #94 from clue-labs/error-handler · reactphp/child-process@51f190c · GitHub
[go: up one dir, main page]

Skip to content

Commit 51f190c

Browse files
authored
Merge pull request #94 from clue-labs/error-handler
Improve error reporting when custom error handler is used
2 parents 8917b92 + 396541d commit 51f190c

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/Process.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,20 @@ public function start(LoopInterface $loop = null, $interval = 0.1)
199199
$options['suppress_errors'] = true;
200200
}
201201

202+
$errstr = '';
203+
\set_error_handler(function ($_, $error) use (&$errstr) {
204+
// Match errstr from PHP's warning message.
205+
// proc_open(/dev/does-not-exist): Failed to open stream: No such file or directory
206+
$errstr = $error;
207+
});
208+
209+
$pipes = array();
202210
$this->process = @\proc_open($cmd, $fdSpec, $pipes, $this->cwd, $this->env, $options);
203211

212+
\restore_error_handler();
213+
204214
if (!\is_resource($this->process)) {
205-
$error = \error_get_last();
206-
throw new \RuntimeException('Unable to launch a new process: ' . $error['message']);
215+
throw new \RuntimeException('Unable to launch a new process: ' . $errstr);
207216
}
208217

209218
// count open process pipes and await close event for each to drain buffers before detecting exit

tests/AbstractProcessTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function testStartWithCustomPipesWillAssignPipes()
126126
$this->assertInstanceOf('React\Stream\WritableStreamInterface', $process->pipes[3]);
127127
}
128128

129-
public function testStartWithInvalidFileDescriptorPathWillThrow()
129+
public function testStartWithInvalidFileDescriptorPathWillThrowWithoutCallingCustomErrorHandler()
130130
{
131131
if (defined('HHVM_VERSION')) {
132132
$this->markTestSkipped('Not supported on legacy HHVM');
@@ -138,8 +138,22 @@ public function testStartWithInvalidFileDescriptorPathWillThrow()
138138

139139
$process = new Process('exit 0', null, null, $fds);
140140

141+
$error = null;
142+
set_error_handler(function ($_, $errstr) use (&$error) {
143+
$error = $errstr;
144+
});
145+
141146
$this->setExpectedException('RuntimeException', 'No such file or directory');
142-
$process->start($this->createLoop());
147+
148+
try {
149+
$process->start($this->createLoop());
150+
restore_error_handler();
151+
} catch (\Exception $e) {
152+
restore_error_handler();
153+
$this->assertNull($error);
154+
155+
throw $e;
156+
}
143157
}
144158

145159
public function testStartWithExcessiveNumberOfFileDescriptorsWillThrow()

0 commit comments

Comments
 (0)
0