Unwrap errors in FlattenException.
This commit is contained in:
parent
7a461cff0c
commit
f14d7d6849
@ -18,21 +18,22 @@ namespace Symfony\Component\Debug\Exception;
|
|||||||
*/
|
*/
|
||||||
class FatalThrowableError extends FatalErrorException
|
class FatalThrowableError extends FatalErrorException
|
||||||
{
|
{
|
||||||
|
private $originalClassName;
|
||||||
|
|
||||||
public function __construct(\Throwable $e)
|
public function __construct(\Throwable $e)
|
||||||
{
|
{
|
||||||
|
$this->originalClassName = \get_class($e);
|
||||||
|
|
||||||
if ($e instanceof \ParseError) {
|
if ($e instanceof \ParseError) {
|
||||||
$message = 'Parse error: '.$e->getMessage();
|
|
||||||
$severity = E_PARSE;
|
$severity = E_PARSE;
|
||||||
} elseif ($e instanceof \TypeError) {
|
} elseif ($e instanceof \TypeError) {
|
||||||
$message = 'Type error: '.$e->getMessage();
|
|
||||||
$severity = E_RECOVERABLE_ERROR;
|
$severity = E_RECOVERABLE_ERROR;
|
||||||
} else {
|
} else {
|
||||||
$message = $e->getMessage();
|
|
||||||
$severity = E_ERROR;
|
$severity = E_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
\ErrorException::__construct(
|
\ErrorException::__construct(
|
||||||
$message,
|
$e->getMessage(),
|
||||||
$e->getCode(),
|
$e->getCode(),
|
||||||
$severity,
|
$severity,
|
||||||
$e->getFile(),
|
$e->getFile(),
|
||||||
@ -41,4 +42,9 @@ class FatalThrowableError extends FatalErrorException
|
|||||||
|
|
||||||
$this->setTrace($e->getTrace());
|
$this->setTrace($e->getTrace());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getOriginalClassName(): string
|
||||||
|
{
|
||||||
|
return $this->originalClassName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ class FlattenException
|
|||||||
$e->setStatusCode($statusCode);
|
$e->setStatusCode($statusCode);
|
||||||
$e->setHeaders($headers);
|
$e->setHeaders($headers);
|
||||||
$e->setTraceFromException($exception);
|
$e->setTraceFromException($exception);
|
||||||
$e->setClass(get_class($exception));
|
$e->setClass($exception instanceof FatalThrowableError ? $exception->getOriginalClassName() : \get_class($exception));
|
||||||
$e->setFile($exception->getFile());
|
$e->setFile($exception->getFile());
|
||||||
$e->setLine($exception->getLine());
|
$e->setLine($exception->getLine());
|
||||||
|
|
||||||
@ -157,7 +157,7 @@ class FlattenException
|
|||||||
return $this->previous;
|
return $this->previous;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setPrevious(FlattenException $previous)
|
public function setPrevious(self $previous)
|
||||||
{
|
{
|
||||||
$this->previous = $previous;
|
$this->previous = $previous;
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
namespace Symfony\Component\Debug\Tests\Exception;
|
namespace Symfony\Component\Debug\Tests\Exception;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Symfony\Component\Debug\Exception\FatalThrowableError;
|
||||||
use Symfony\Component\Debug\Exception\FlattenException;
|
use Symfony\Component\Debug\Exception\FlattenException;
|
||||||
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
|
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
|
||||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
@ -123,6 +124,16 @@ class FlattenExceptionTest extends TestCase
|
|||||||
$this->assertInstanceOf($flattened->getClass(), $exception, 'The class is set to the class of the original exception');
|
$this->assertInstanceOf($flattened->getClass(), $exception, 'The class is set to the class of the original exception');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testThrowable()
|
||||||
|
{
|
||||||
|
$exception = new FatalThrowableError(new \DivisionByZeroError('Ouch', 42));
|
||||||
|
$flattened = FlattenException::create($exception);
|
||||||
|
|
||||||
|
$this->assertSame('Ouch', $flattened->getMessage(), 'The message is copied from the original error.');
|
||||||
|
$this->assertSame(42, $flattened->getCode(), 'The code is copied from the original error.');
|
||||||
|
$this->assertSame('DivisionByZeroError', $flattened->getClass(), 'The class is set to the class of the original error');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider flattenDataProvider
|
* @dataProvider flattenDataProvider
|
||||||
*/
|
*/
|
||||||
@ -144,9 +155,9 @@ class FlattenExceptionTest extends TestCase
|
|||||||
|
|
||||||
$flattened = FlattenException::create($exception)->getPrevious();
|
$flattened = FlattenException::create($exception)->getPrevious();
|
||||||
|
|
||||||
$this->assertEquals($flattened->getMessage(), 'Parse error: Oh noes!', 'The message is copied from the original exception.');
|
$this->assertEquals($flattened->getMessage(), 'Oh noes!', 'The message is copied from the original exception.');
|
||||||
$this->assertEquals($flattened->getCode(), 42, 'The code is copied from the original exception.');
|
$this->assertEquals($flattened->getCode(), 42, 'The code is copied from the original exception.');
|
||||||
$this->assertEquals($flattened->getClass(), 'Symfony\Component\Debug\Exception\FatalThrowableError', 'The class is set to the class of the original exception');
|
$this->assertEquals($flattened->getClass(), 'ParseError', 'The class is set to the class of the original exception');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user