bug #14469 [Debug] BaseException compatibility for PHP7 (nicolas-grekas)
This PR was merged into the 2.6 branch.
Discussion
----------
[Debug] BaseException compatibility for PHP7
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
Commits
-------
e94b31f
[Debug] PHP7 compatibility with BaseException
This commit is contained in:
commit
13ae7fae09
@ -14,6 +14,7 @@ namespace Symfony\Component\Debug;
|
|||||||
use Psr\Log\LogLevel;
|
use Psr\Log\LogLevel;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Symfony\Component\Debug\Exception\ContextErrorException;
|
use Symfony\Component\Debug\Exception\ContextErrorException;
|
||||||
|
use Symfony\Component\Debug\Exception\FatalBaseException;
|
||||||
use Symfony\Component\Debug\Exception\FatalErrorException;
|
use Symfony\Component\Debug\Exception\FatalErrorException;
|
||||||
use Symfony\Component\Debug\Exception\OutOfMemoryException;
|
use Symfony\Component\Debug\Exception\OutOfMemoryException;
|
||||||
use Symfony\Component\Debug\FatalErrorHandler\UndefinedFunctionFatalErrorHandler;
|
use Symfony\Component\Debug\FatalErrorHandler\UndefinedFunctionFatalErrorHandler;
|
||||||
@ -430,22 +431,34 @@ class ErrorHandler
|
|||||||
/**
|
/**
|
||||||
* Handles an exception by logging then forwarding it to an other handler.
|
* Handles an exception by logging then forwarding it to an other handler.
|
||||||
*
|
*
|
||||||
* @param \Exception $exception An exception to handle
|
* @param \Exception|\BaseException $exception An exception to handle
|
||||||
* @param array $error An array as returned by error_get_last()
|
* @param array $error An array as returned by error_get_last()
|
||||||
*
|
*
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
public function handleException(\Exception $exception, array $error = null)
|
public function handleException($exception, array $error = null)
|
||||||
{
|
{
|
||||||
if ($this->loggedErrors & E_ERROR) {
|
if (!$exception instanceof \Exception) {
|
||||||
|
$exception = new FatalBaseException($exception);
|
||||||
|
}
|
||||||
|
$type = $exception instanceof FatalErrorException ? $exception->getSeverity() : E_ERROR;
|
||||||
|
|
||||||
|
if ($this->loggedErrors & $type) {
|
||||||
$e = array(
|
$e = array(
|
||||||
'type' => E_ERROR,
|
'type' => $type,
|
||||||
'file' => $exception->getFile(),
|
'file' => $exception->getFile(),
|
||||||
'line' => $exception->getLine(),
|
'line' => $exception->getLine(),
|
||||||
'level' => error_reporting(),
|
'level' => error_reporting(),
|
||||||
'stack' => $exception->getTrace(),
|
'stack' => $exception->getTrace(),
|
||||||
);
|
);
|
||||||
if ($exception instanceof FatalErrorException) {
|
if ($exception instanceof FatalBaseException) {
|
||||||
|
$error = array(
|
||||||
|
'type' => $type,
|
||||||
|
'message' => $message = $exception->getMessage(),
|
||||||
|
'file' => $e['file'],
|
||||||
|
'line' => $e['line'],
|
||||||
|
);
|
||||||
|
} elseif ($exception instanceof FatalErrorException) {
|
||||||
$message = 'Fatal '.$exception->getMessage();
|
$message = 'Fatal '.$exception->getMessage();
|
||||||
} elseif ($exception instanceof \ErrorException) {
|
} elseif ($exception instanceof \ErrorException) {
|
||||||
$message = 'Uncaught '.$exception->getMessage();
|
$message = 'Uncaught '.$exception->getMessage();
|
||||||
@ -475,6 +488,9 @@ class ErrorHandler
|
|||||||
} catch (\Exception $handlerException) {
|
} catch (\Exception $handlerException) {
|
||||||
$this->exceptionHandler = null;
|
$this->exceptionHandler = null;
|
||||||
$this->handleException($handlerException);
|
$this->handleException($handlerException);
|
||||||
|
} catch (\BaseException $handlerException) {
|
||||||
|
$this->exceptionHandler = null;
|
||||||
|
$this->handleException($handlerException);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
44
src/Symfony/Component/Debug/Exception/FatalBaseException.php
Normal file
44
src/Symfony/Component/Debug/Exception/FatalBaseException.php
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Debug\Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base Fatal Error Exception.
|
||||||
|
*
|
||||||
|
* @author Nicolas Grekas <p@tchwork.com>
|
||||||
|
*/
|
||||||
|
class FatalBaseException extends FatalErrorException
|
||||||
|
{
|
||||||
|
public function __construct(\BaseException $e)
|
||||||
|
{
|
||||||
|
if ($e instanceof \ParseException) {
|
||||||
|
$message = 'Parse error: '.$e->getMessage();
|
||||||
|
$severity = E_PARSE;
|
||||||
|
} elseif ($e instanceof \TypeException) {
|
||||||
|
$message = 'Type error: '.$e->getMessage();
|
||||||
|
$severity = E_RECOVERABLE_ERROR;
|
||||||
|
} else {
|
||||||
|
$message = 'Fatal error: '.$e->getMessage();
|
||||||
|
$severity = E_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
\ErrorException::__construct(
|
||||||
|
$message,
|
||||||
|
$e->getCode(),
|
||||||
|
$severity,
|
||||||
|
$e->getFile(),
|
||||||
|
$e->getLine()
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->setTrace($e->getTrace());
|
||||||
|
}
|
||||||
|
}
|
@ -104,9 +104,14 @@ class DebugClassLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
// if an exception is thrown, the test passed
|
// if an exception is thrown, the test passed
|
||||||
restore_error_handler();
|
restore_error_handler();
|
||||||
restore_exception_handler();
|
restore_exception_handler();
|
||||||
$this->assertEquals(E_STRICT, $exception->getSeverity());
|
|
||||||
$this->assertStringStartsWith(__FILE__, $exception->getFile());
|
$this->assertStringStartsWith(__FILE__, $exception->getFile());
|
||||||
$this->assertRegexp('/^Runtime Notice: Declaration/', $exception->getMessage());
|
if (PHP_VERSION_ID < 70000) {
|
||||||
|
$this->assertRegexp('/^Runtime Notice: Declaration/', $exception->getMessage());
|
||||||
|
$this->assertEquals(E_STRICT, $exception->getSeverity());
|
||||||
|
} else {
|
||||||
|
$this->assertRegexp('/^Warning: Declaration/', $exception->getMessage());
|
||||||
|
$this->assertEquals(E_WARNING, $exception->getSeverity());
|
||||||
|
}
|
||||||
} catch (\Exception $exception) {
|
} catch (\Exception $exception) {
|
||||||
restore_error_handler();
|
restore_error_handler();
|
||||||
restore_exception_handler();
|
restore_exception_handler();
|
||||||
|
@ -336,7 +336,7 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
|
|||||||
$logArgCheck = function ($level, $message, $context) use ($that) {
|
$logArgCheck = function ($level, $message, $context) use ($that) {
|
||||||
$that->assertEquals('Fatal Parse Error: foo', $message);
|
$that->assertEquals('Fatal Parse Error: foo', $message);
|
||||||
$that->assertArrayHasKey('type', $context);
|
$that->assertArrayHasKey('type', $context);
|
||||||
$that->assertEquals($context['type'], E_ERROR);
|
$that->assertEquals($context['type'], E_PARSE);
|
||||||
};
|
};
|
||||||
|
|
||||||
$logger
|
$logger
|
||||||
@ -345,7 +345,7 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
|
|||||||
->will($this->returnCallback($logArgCheck))
|
->will($this->returnCallback($logArgCheck))
|
||||||
;
|
;
|
||||||
|
|
||||||
$handler->setDefaultLogger($logger, E_ERROR);
|
$handler->setDefaultLogger($logger, E_PARSE);
|
||||||
|
|
||||||
$handler->handleFatalError($error);
|
$handler->handleFatalError($error);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user