Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4813746

Browse filesBrowse files
committed
Support any Throwable object in FlattenException.
1 parent 9e82562 commit 4813746
Copy full SHA for 4813746

File tree

2 files changed

+64
-25
lines changed
Filter options

2 files changed

+64
-25
lines changed

‎src/Symfony/Component/Debug/Exception/FlattenException.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Debug/Exception/FlattenException.php
+39-17Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
1616

1717
/**
18-
* FlattenException wraps a PHP Exception to be able to serialize it.
18+
* FlattenException wraps a PHP Error or Exception to be able to serialize it.
1919
*
2020
* Basically, this class removes all objects from the trace.
2121
*
@@ -33,16 +33,30 @@ class FlattenException
3333
private $file;
3434
private $line;
3535

36-
public static function create(\Exception $exception, $statusCode = null, array $headers = array())
36+
/**
37+
* @param \Throwable $throwable
38+
* @param int|null $statusCode
39+
* @param array $headers
40+
*
41+
* @return static
42+
*/
43+
public static function create($throwable, $statusCode = null, array $headers = array())
3744
{
45+
if (!$throwable instanceof \Throwable) {
46+
throw new \InvalidArgumentException(sprintf(
47+
'Expected instance of %s, got %s.',
48+
is_object($throwable) ? get_class($throwable) : gettype($throwable)
49+
));
50+
}
51+
3852
$e = new static();
39-
$e->setMessage($exception->getMessage());
40-
$e->setCode($exception->getCode());
53+
$e->setMessage($throwable->getMessage());
54+
$e->setCode($throwable->getCode());
4155

42-
if ($exception instanceof HttpExceptionInterface) {
43-
$statusCode = $exception->getStatusCode();
44-
$headers = array_merge($headers, $exception->getHeaders());
45-
} elseif ($exception instanceof RequestExceptionInterface) {
56+
if ($throwable instanceof HttpExceptionInterface) {
57+
$statusCode = $throwable->getStatusCode();
58+
$headers = array_merge($headers, $throwable->getHeaders());
59+
} elseif ($throwable instanceof RequestExceptionInterface) {
4660
$statusCode = 400;
4761
}
4862

@@ -52,17 +66,15 @@ public static function create(\Exception $exception, $statusCode = null, array $
5266

5367
$e->setStatusCode($statusCode);
5468
$e->setHeaders($headers);
55-
$e->setTraceFromException($exception);
56-
$e->setClass($exception instanceof FatalThrowableError ? $exception->getOriginalClassName() : \get_class($exception));
57-
$e->setFile($exception->getFile());
58-
$e->setLine($exception->getLine());
69+
$e->setTraceFromThrowable($throwable);
70+
$e->setClass($throwable instanceof FatalThrowableError ? $throwable->getOriginalClassName() : \get_class($throwable));
71+
$e->setFile($throwable->getFile());
72+
$e->setLine($throwable->getLine());
5973

60-
$previous = $exception->getPrevious();
74+
$previous = $throwable->getPrevious();
6175

62-
if ($previous instanceof \Exception) {
76+
if ($previous instanceof \Throwable) {
6377
$e->setPrevious(static::create($previous));
64-
} elseif ($previous instanceof \Throwable) {
65-
$e->setPrevious(static::create(new FatalThrowableError($previous)));
6678
}
6779

6880
return $e;
@@ -178,9 +190,19 @@ public function getTrace()
178190
return $this->trace;
179191
}
180192

193+
/**
194+
* @deprecated since 4.1, use {@see setTraceFromThrowable()} instead.
195+
*/
181196
public function setTraceFromException(\Exception $exception)
182197
{
183-
$this->setTrace($exception->getTrace(), $exception->getFile(), $exception->getLine());
198+
@trigger_error(sprintf('"%s" is deprecated since Symfony 4.1. Use "setTraceFromThrowable()" instead.', __METHOD__), E_USER_DEPRECATED);
199+
200+
$this->setTraceFromThrowable($exception);
201+
}
202+
203+
public function setTraceFromThrowable(\Throwable $throwable): void
204+
{
205+
$this->setTrace($throwable->getTrace(), $throwable->getFile(), $throwable->getLine());
184206
}
185207

186208
public function setTrace($trace, $file, $line)

‎src/Symfony/Component/Debug/Tests/Exception/FlattenExceptionTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Debug/Tests/Exception/FlattenExceptionTest.php
+25-8Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ public function testStatusCode()
4040
$flattened = FlattenException::create(new \RuntimeException());
4141
$this->assertEquals('500', $flattened->getStatusCode());
4242

43+
$flattened = FlattenException::create(new \DivisionByZeroError(), 403);
44+
$this->assertEquals('403', $flattened->getStatusCode());
45+
46+
$flattened = FlattenException::create(new \DivisionByZeroError());
47+
$this->assertEquals('500', $flattened->getStatusCode());
48+
4349
$flattened = FlattenException::create(new NotFoundHttpException());
4450
$this->assertEquals('404', $flattened->getStatusCode());
4551

@@ -112,7 +118,7 @@ public function testHeadersForHttpException()
112118
/**
113119
* @dataProvider flattenDataProvider
114120
*/
115-
public function testFlattenHttpException(\Exception $exception)
121+
public function testFlattenHttpException(\Throwable $exception)
116122
{
117123
$flattened = FlattenException::create($exception);
118124
$flattened2 = FlattenException::create($exception);
@@ -124,7 +130,7 @@ public function testFlattenHttpException(\Exception $exception)
124130
$this->assertInstanceOf($flattened->getClass(), $exception, 'The class is set to the class of the original exception');
125131
}
126132

127-
public function testThrowable()
133+
public function testWrappedThrowable()
128134
{
129135
$exception = new FatalThrowableError(new \DivisionByZeroError('Ouch', 42));
130136
$flattened = FlattenException::create($exception);
@@ -134,10 +140,20 @@ public function testThrowable()
134140
$this->assertSame('DivisionByZeroError', $flattened->getClass(), 'The class is set to the class of the original error');
135141
}
136142

143+
public function testThrowable()
144+
{
145+
$exception = new \DivisionByZeroError('Ouch', 42);
146+
$flattened = FlattenException::create($exception);
147+
148+
$this->assertSame('Ouch', $flattened->getMessage(), 'The message is copied from the original error.');
149+
$this->assertSame(42, $flattened->getCode(), 'The code is copied from the original error.');
150+
$this->assertSame('DivisionByZeroError', $flattened->getClass(), 'The class is set to the class of the original error');
151+
}
152+
137153
/**
138154
* @dataProvider flattenDataProvider
139155
*/
140-
public function testPrevious(\Exception $exception)
156+
public function testPrevious(\Throwable $exception)
141157
{
142158
$flattened = FlattenException::create($exception);
143159
$flattened2 = FlattenException::create($exception);
@@ -163,7 +179,7 @@ public function testPreviousError()
163179
/**
164180
* @dataProvider flattenDataProvider
165181
*/
166-
public function testLine(\Exception $exception)
182+
public function testLine(\Throwable $exception)
167183
{
168184
$flattened = FlattenException::create($exception);
169185
$this->assertSame($exception->getLine(), $flattened->getLine());
@@ -172,7 +188,7 @@ public function testLine(\Exception $exception)
172188
/**
173189
* @dataProvider flattenDataProvider
174190
*/
175-
public function testFile(\Exception $exception)
191+
public function testFile(\Throwable $exception)
176192
{
177193
$flattened = FlattenException::create($exception);
178194
$this->assertSame($exception->getFile(), $flattened->getFile());
@@ -181,15 +197,15 @@ public function testFile(\Exception $exception)
181197
/**
182198
* @dataProvider flattenDataProvider
183199
*/
184-
public function testToArray(\Exception $exception)
200+
public function testToArray(\Throwable $exception, string $expectedClass)
185201
{
186202
$flattened = FlattenException::create($exception);
187203
$flattened->setTrace(array(), 'foo.php', 123);
188204

189205
$this->assertEquals(array(
190206
array(
191207
'message' => 'test',
192-
'class' => 'Exception',
208+
'class' => $expectedClass,
193209
'trace' => array(array(
194210
'namespace' => '', 'short_class' => '', 'class' => '', 'type' => '', 'function' => '', 'file' => 'foo.php', 'line' => 123,
195211
'args' => array(),
@@ -201,7 +217,8 @@ public function testToArray(\Exception $exception)
201217
public function flattenDataProvider()
202218
{
203219
return array(
204-
array(new \Exception('test', 123)),
220+
array(new \Exception('test', 123), 'Exception'),
221+
array(new \Error('test', 123), 'Error')
205222
);
206223
}
207224

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.