Skip to content

Commit

Permalink
Merge pull request #94 from clue-labs/error-handler
Browse files Browse the repository at this point in the history
Improve error reporting when custom error handler is used
  • Loading branch information
WyriHaximus committed Apr 19, 2022
2 parents 8917b92 + 396541d commit 51f190c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
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

0 comments on commit 51f190c

Please sign in to comment.