Skip to content

[Debug] Support any Throwable object in FlattenException #26528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/Symfony/Component/Debug/Exception/FlattenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

/**
* FlattenException wraps a PHP Exception to be able to serialize it.
* FlattenException wraps a PHP Error or Exception to be able to serialize it.
*
* Basically, this class removes all objects from the trace.
*
Expand All @@ -34,6 +34,11 @@ class FlattenException
private $line;

public static function create(\Exception $exception, $statusCode = null, array $headers = array())
{
return static::createFromThrowable($exception, $statusCode, $headers);
}

public static function createFromThrowable(\Throwable $exception, ?int $statusCode = null, array $headers = array()): self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using new instead (FlattenException::new() looks better to me)?

Copy link
Member

@nicolas-grekas nicolas-grekas Apr 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not possible to call a function "new" because the keyword is reserved...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works in php 7 https://3v4l.org/N5iEr

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, then the developer would have the choice between create() and new() which looks rather confusing to me. Also, I would not recommend to use keywords as method names even if it currently works.

{
$e = new static();
$e->setMessage($exception->getMessage());
Expand All @@ -52,17 +57,15 @@ public static function create(\Exception $exception, $statusCode = null, array $

$e->setStatusCode($statusCode);
$e->setHeaders($headers);
$e->setTraceFromException($exception);
$e->setTraceFromThrowable($exception);
$e->setClass($exception instanceof FatalThrowableError ? $exception->getOriginalClassName() : \get_class($exception));
$e->setFile($exception->getFile());
$e->setLine($exception->getLine());

$previous = $exception->getPrevious();

if ($previous instanceof \Exception) {
$e->setPrevious(static::create($previous));
} elseif ($previous instanceof \Throwable) {
$e->setPrevious(static::create(new FatalThrowableError($previous)));
if ($previous instanceof \Throwable) {
$e->setPrevious(static::createFromThrowable($previous));
}

return $e;
Expand Down Expand Up @@ -178,9 +181,19 @@ public function getTrace()
return $this->trace;
}

/**
* @deprecated since 4.1, use {@see setTraceFromThrowable()} instead.
*/
public function setTraceFromException(\Exception $exception)
{
$this->setTrace($exception->getTrace(), $exception->getFile(), $exception->getLine());
@trigger_error(sprintf('"%s" is deprecated since Symfony 4.1, use "setTraceFromThrowable()" instead.', __METHOD__), E_USER_DEPRECATED);

$this->setTraceFromThrowable($exception);
}

public function setTraceFromThrowable(\Throwable $throwable): void
{
$this->setTrace($throwable->getTrace(), $throwable->getFile(), $throwable->getLine());
}

public function setTrace($trace, $file, $line)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public function testStatusCode()
$flattened = FlattenException::create(new \RuntimeException());
$this->assertEquals('500', $flattened->getStatusCode());

$flattened = FlattenException::createFromThrowable(new \DivisionByZeroError(), 403);
$this->assertEquals('403', $flattened->getStatusCode());

$flattened = FlattenException::createFromThrowable(new \DivisionByZeroError());
$this->assertEquals('500', $flattened->getStatusCode());

$flattened = FlattenException::create(new NotFoundHttpException());
$this->assertEquals('404', $flattened->getStatusCode());

Expand Down Expand Up @@ -112,10 +118,10 @@ public function testHeadersForHttpException()
/**
* @dataProvider flattenDataProvider
*/
public function testFlattenHttpException(\Exception $exception)
public function testFlattenHttpException(\Throwable $exception)
{
$flattened = FlattenException::create($exception);
$flattened2 = FlattenException::create($exception);
$flattened = FlattenException::createFromThrowable($exception);
$flattened2 = FlattenException::createFromThrowable($exception);

$flattened->setPrevious($flattened2);

Expand All @@ -124,7 +130,7 @@ public function testFlattenHttpException(\Exception $exception)
$this->assertInstanceOf($flattened->getClass(), $exception, 'The class is set to the class of the original exception');
}

public function testThrowable()
public function testWrappedThrowable()
{
$exception = new FatalThrowableError(new \DivisionByZeroError('Ouch', 42));
$flattened = FlattenException::create($exception);
Expand All @@ -134,13 +140,23 @@ public function testThrowable()
$this->assertSame('DivisionByZeroError', $flattened->getClass(), 'The class is set to the class of the original error');
}

public function testThrowable()
{
$error = new \DivisionByZeroError('Ouch', 42);
$flattened = FlattenException::createFromThrowable($error);

$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
*/
public function testPrevious(\Exception $exception)
public function testPrevious(\Throwable $exception)
{
$flattened = FlattenException::create($exception);
$flattened2 = FlattenException::create($exception);
$flattened = FlattenException::createFromThrowable($exception);
$flattened2 = FlattenException::createFromThrowable($exception);

$flattened->setPrevious($flattened2);

Expand All @@ -163,33 +179,33 @@ public function testPreviousError()
/**
* @dataProvider flattenDataProvider
*/
public function testLine(\Exception $exception)
public function testLine(\Throwable $exception)
{
$flattened = FlattenException::create($exception);
$flattened = FlattenException::createFromThrowable($exception);
$this->assertSame($exception->getLine(), $flattened->getLine());
}

/**
* @dataProvider flattenDataProvider
*/
public function testFile(\Exception $exception)
public function testFile(\Throwable $exception)
{
$flattened = FlattenException::create($exception);
$flattened = FlattenException::createFromThrowable($exception);
$this->assertSame($exception->getFile(), $flattened->getFile());
}

/**
* @dataProvider flattenDataProvider
*/
public function testToArray(\Exception $exception)
public function testToArray(\Throwable $exception, string $expectedClass)
{
$flattened = FlattenException::create($exception);
$flattened = FlattenException::createFromThrowable($exception);
$flattened->setTrace(array(), 'foo.php', 123);

$this->assertEquals(array(
array(
'message' => 'test',
'class' => 'Exception',
'class' => $expectedClass,
'trace' => array(array(
'namespace' => '', 'short_class' => '', 'class' => '', 'type' => '', 'function' => '', 'file' => 'foo.php', 'line' => 123,
'args' => array(),
Expand All @@ -198,10 +214,24 @@ public function testToArray(\Exception $exception)
), $flattened->toArray());
}

public function testCreate()
{
$exception = new NotFoundHttpException(
'test',
new \RuntimeException('previous', 123)
);

$this->assertSame(
FlattenException::createFromThrowable($exception)->toArray(),
FlattenException::create($exception)->toArray()
);
}

public function flattenDataProvider()
{
return array(
array(new \Exception('test', 123)),
array(new \Exception('test', 123), 'Exception'),
array(new \Error('test', 123), 'Error'),
);
}

Expand Down