Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 47e2bd3

Browse files
committed
feature #26528 [Debug] Support any Throwable object in FlattenException (derrabus)
This PR was merged into the 4.1-dev branch. Discussion ---------- [Debug] Support any Throwable object in FlattenException | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #26429 | License | MIT | Doc PR | N/A Alternative to #26447 without BC breaks, follows #26028. This PR removes the need to convert `Throwable` objects into exceptions when working with `FlattenException`. ping @instabledesign @ostrolucky @nicolas-grekas Commits ------- 96c1a6f Support any Throwable object in FlattenException.
2 parents 60e48cb + 96c1a6f commit 47e2bd3

File tree

2 files changed

+65
-22
lines changed

2 files changed

+65
-22
lines changed

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

Lines changed: 20 additions & 7 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
*
@@ -34,6 +34,11 @@ class FlattenException
3434
private $line;
3535

3636
public static function create(\Exception $exception, $statusCode = null, array $headers = array())
37+
{
38+
return static::createFromThrowable($exception, $statusCode, $headers);
39+
}
40+
41+
public static function createFromThrowable(\Throwable $exception, ?int $statusCode = null, array $headers = array()): self
3742
{
3843
$e = new static();
3944
$e->setMessage($exception->getMessage());
@@ -52,17 +57,15 @@ public static function create(\Exception $exception, $statusCode = null, array $
5257

5358
$e->setStatusCode($statusCode);
5459
$e->setHeaders($headers);
55-
$e->setTraceFromException($exception);
60+
$e->setTraceFromThrowable($exception);
5661
$e->setClass($exception instanceof FatalThrowableError ? $exception->getOriginalClassName() : \get_class($exception));
5762
$e->setFile($exception->getFile());
5863
$e->setLine($exception->getLine());
5964

6065
$previous = $exception->getPrevious();
6166

62-
if ($previous instanceof \Exception) {
63-
$e->setPrevious(static::create($previous));
64-
} elseif ($previous instanceof \Throwable) {
65-
$e->setPrevious(static::create(new FatalThrowableError($previous)));
67+
if ($previous instanceof \Throwable) {
68+
$e->setPrevious(static::createFromThrowable($previous));
6669
}
6770

6871
return $e;
@@ -178,9 +181,19 @@ public function getTrace()
178181
return $this->trace;
179182
}
180183

184+
/**
185+
* @deprecated since 4.1, use {@see setTraceFromThrowable()} instead.
186+
*/
181187
public function setTraceFromException(\Exception $exception)
182188
{
183-
$this->setTrace($exception->getTrace(), $exception->getFile(), $exception->getLine());
189+
@trigger_error(sprintf('"%s" is deprecated since Symfony 4.1, use "setTraceFromThrowable()" instead.', __METHOD__), E_USER_DEPRECATED);
190+
191+
$this->setTraceFromThrowable($exception);
192+
}
193+
194+
public function setTraceFromThrowable(\Throwable $throwable): void
195+
{
196+
$this->setTrace($throwable->getTrace(), $throwable->getFile(), $throwable->getLine());
184197
}
185198

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

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

Lines changed: 45 additions & 15 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::createFromThrowable(new \DivisionByZeroError(), 403);
44+
$this->assertEquals('403', $flattened->getStatusCode());
45+
46+
$flattened = FlattenException::createFromThrowable(new \DivisionByZeroError());
47+
$this->assertEquals('500', $flattened->getStatusCode());
48+
4349
$flattened = FlattenException::create(new NotFoundHttpException());
4450
$this->assertEquals('404', $flattened->getStatusCode());
4551

@@ -112,10 +118,10 @@ public function testHeadersForHttpException()
112118
/**
113119
* @dataProvider flattenDataProvider
114120
*/
115-
public function testFlattenHttpException(\Exception $exception)
121+
public function testFlattenHttpException(\Throwable $exception)
116122
{
117-
$flattened = FlattenException::create($exception);
118-
$flattened2 = FlattenException::create($exception);
123+
$flattened = FlattenException::createFromThrowable($exception);
124+
$flattened2 = FlattenException::createFromThrowable($exception);
119125

120126
$flattened->setPrevious($flattened2);
121127

@@ -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,13 +140,23 @@ 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+
$error = new \DivisionByZeroError('Ouch', 42);
146+
$flattened = FlattenException::createFromThrowable($error);
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
{
142-
$flattened = FlattenException::create($exception);
143-
$flattened2 = FlattenException::create($exception);
158+
$flattened = FlattenException::createFromThrowable($exception);
159+
$flattened2 = FlattenException::createFromThrowable($exception);
144160

145161
$flattened->setPrevious($flattened2);
146162

@@ -163,33 +179,33 @@ public function testPreviousError()
163179
/**
164180
* @dataProvider flattenDataProvider
165181
*/
166-
public function testLine(\Exception $exception)
182+
public function testLine(\Throwable $exception)
167183
{
168-
$flattened = FlattenException::create($exception);
184+
$flattened = FlattenException::createFromThrowable($exception);
169185
$this->assertSame($exception->getLine(), $flattened->getLine());
170186
}
171187

172188
/**
173189
* @dataProvider flattenDataProvider
174190
*/
175-
public function testFile(\Exception $exception)
191+
public function testFile(\Throwable $exception)
176192
{
177-
$flattened = FlattenException::create($exception);
193+
$flattened = FlattenException::createFromThrowable($exception);
178194
$this->assertSame($exception->getFile(), $flattened->getFile());
179195
}
180196

181197
/**
182198
* @dataProvider flattenDataProvider
183199
*/
184-
public function testToArray(\Exception $exception)
200+
public function testToArray(\Throwable $exception, string $expectedClass)
185201
{
186-
$flattened = FlattenException::create($exception);
202+
$flattened = FlattenException::createFromThrowable($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(),
@@ -198,10 +214,24 @@ public function testToArray(\Exception $exception)
198214
), $flattened->toArray());
199215
}
200216

217+
public function testCreate()
218+
{
219+
$exception = new NotFoundHttpException(
220+
'test',
221+
new \RuntimeException('previous', 123)
222+
);
223+
224+
$this->assertSame(
225+
FlattenException::createFromThrowable($exception)->toArray(),
226+
FlattenException::create($exception)->toArray()
227+
);
228+
}
229+
201230
public function flattenDataProvider()
202231
{
203232
return array(
204-
array(new \Exception('test', 123)),
233+
array(new \Exception('test', 123), 'Exception'),
234+
array(new \Error('test', 123), 'Error'),
205235
);
206236
}
207237

0 commit comments

Comments
 (0)