1
0
Fork 0

Added ErrorHandler for handling E_NOTICE, E_WARNING, E_ERROR

pull/263/head
Artem Lopata 2012-01-27 10:29:07 +02:00
parent 54dece1dc7
commit 7d994b5de4
3 changed files with 103 additions and 0 deletions

View File

@ -24,6 +24,7 @@ use Composer\Composer;
use Composer\Factory;
use Composer\IO\IOInterface;
use Composer\IO\ConsoleIO;
use Composer\Util\ErrorHandler;
/**
* The console application that handles the commands
@ -39,6 +40,7 @@ class Application extends BaseApplication
public function __construct()
{
ErrorHandler::set();
parent::__construct('Composer', Composer::VERSION);
}

View File

@ -0,0 +1,52 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Util;
/**
* Convert PHP E_NOTICE, E_WARNING into exceptions
*
* @author Artem Lopata <biozshock@gmail.com>
*/
class ErrorHandler
{
/**
* Error handler
*
* @param int $errorNo Level of the error raised
* @param string $errorString Error message
* @param string $errorFile Filename that the error was raised in
* @param int $errorLine Line number the error was raised at
*
* @static
* @throws \ErrorException
*/
public static function handle($errorNo, $errorString, $errorFile, $errorLine)
{
//this allows error suppression in 3rd party code to work
if (!error_reporting()) {
return;
}
throw new \ErrorException(sprintf('%s in %s:%d', $errorString, $errorFile, $errorLine), $errorNo);
}
/**
* Set error handler
*
* @static
*/
public static function set()
{
set_error_handler(array(__CLASS__, 'handle'));
}
}

View File

@ -0,0 +1,49 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Test\Util;
use Composer\Util\ErrorHandler;
use Composer\Test\TestCase;
/**
* ErrorHandler test case
*
* @author Artem Lopata <biozshock@gmail.com>
*/
class ErrorHandlerTest extends TestCase
{
/**
* Test ErrorHandler handles notices
*/
public function testErrorHandlerCaptureNotice()
{
$this->setExpectedException('\ErrorException', 'Undefined index: baz in ' . __FILE__);
ErrorHandler::set();
$array = array('foo' => 'bar');
$array['baz'];
}
/**
* Test ErrorHandler handles warnings
*/
public function testErrorHandlerCaptureWarning()
{
$this->setExpectedException('\ErrorException', 'array_merge(): Argument #2 is not an array in ' . __FILE__);
ErrorHandler::set();
array_merge(array(), 'string');
}
}