Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error reporting when custom error handler is used #94

Merged
merged 1 commit into from Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Process.php
Expand Up @@ -199,11 +199,20 @@ public function start(LoopInterface $loop = null, $interval = 0.1)
$options['suppress_errors'] = true;
}

$errstr = '';
\set_error_handler(function ($_, $error) use (&$errstr) {
// Match errstr from PHP's warning message.
// proc_open(/dev/does-not-exist): Failed to open stream: No such file or directory
$errstr = $error;
});

$pipes = array();
$this->process = @\proc_open($cmd, $fdSpec, $pipes, $this->cwd, $this->env, $options);

\restore_error_handler();

if (!\is_resource($this->process)) {
$error = \error_get_last();
throw new \RuntimeException('Unable to launch a new process: ' . $error['message']);
throw new \RuntimeException('Unable to launch a new process: ' . $errstr);
}

// count open process pipes and await close event for each to drain buffers before detecting exit
Expand Down
18 changes: 16 additions & 2 deletions tests/AbstractProcessTest.php
Expand Up @@ -126,7 +126,7 @@ public function testStartWithCustomPipesWillAssignPipes()
$this->assertInstanceOf('React\Stream\WritableStreamInterface', $process->pipes[3]);
}

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

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

$error = null;
set_error_handler(function ($_, $errstr) use (&$error) {
$error = $errstr;
});

$this->setExpectedException('RuntimeException', 'No such file or directory');
$process->start($this->createLoop());

try {
$process->start($this->createLoop());
restore_error_handler();
} catch (\Exception $e) {
restore_error_handler();
$this->assertNull($error);

throw $e;
}
}

public function testStartWithExcessiveNumberOfFileDescriptorsWillThrow()
Expand Down