Skip to content

Commit

Permalink
Merge pull request consolidation#154 from aaukt/errorhandler
Browse files Browse the repository at this point in the history
use custom error handler to correctly handle errors that were suppressed...
  • Loading branch information
DavertMik committed Mar 23, 2015
2 parents 676e031 + 72f1c74 commit 1057a32
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ protected function loadRoboFile()
public function execute($input = null)
{
register_shutdown_function(array($this, 'shutdown'));
set_error_handler(array($this, 'handleError'));
Config::setOutput(new ConsoleOutput());
$input = $this->prepareInput($input ? $input : $_SERVER['argv']);

Expand Down Expand Up @@ -163,5 +164,20 @@ public function shutdown()
if (!is_array($error)) return;
$this->writeln(sprintf("<error>ERROR: %s \nin %s:%d\n</error>", $error['message'], $error['file'], $error['line']));
}

/**
* This is just a proxy error handler that checks the current error_reporting level.
* In case error_reporting is disabled the error is marked as handled, otherwise
* the normal internal error handling resumes.
*
* @return bool
*/
public function handleError()
{
if (error_reporting() === 0) {
return true;
}
return false;
}
}

27 changes: 27 additions & 0 deletions tests/unit/RunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,33 @@ public function testCommandNaming()
$this->assertNotNull($this->app->find('generate:user-avatar'));
}

public function testHandleError()
{
$tmpLevel = error_reporting();

$this->assertFalse($this->runner->handleError());
error_reporting(0);
$this->assertTrue($this->runner->handleError());

error_reporting($tmpLevel);
}

public function testErrorIsHandled()
{
$tmpLevel = error_reporting();

error_reporting(E_USER_ERROR);
set_error_handler(array($this->runner, 'handleError'));
@trigger_error('test error', E_USER_ERROR);
$this->assertEmpty(error_get_last());

error_reporting(0);
trigger_error('test error', E_USER_ERROR);
$this->assertEmpty(error_get_last());

error_reporting($tmpLevel);
}

protected function createCommand($name)
{
return $this->runner->createCommand(new \Robo\TaskInfo(self::ROBOFILE, $name));
Expand Down

0 comments on commit 1057a32

Please sign in to comment.