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

Skip to content

Commit 3fa4f16

Browse files
committed
[Debug] Mimic __toString php behavior in FlattenException
1 parent 2b34f2b commit 3fa4f16

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

src/Symfony/Component/Debug/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
CHANGELOG
22
=========
33

4+
4.2.0
5+
-----
6+
7+
* added `Exception\FlattenException::__toString` and
8+
`Exception\FlattenException::getTraceAsString` to increase compatibility to php
9+
exception objects
10+
411
4.0.0
512
-----
613

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class FlattenException
2727
private $code;
2828
private $previous;
2929
private $trace;
30+
private $traceAsString;
3031
private $class;
3132
private $statusCode;
3233
private $headers;
@@ -239,6 +240,8 @@ public function setTraceFromException(\Exception $exception)
239240

240241
public function setTraceFromThrowable(\Throwable $throwable)
241242
{
243+
$this->traceAsString = $throwable->getTraceAsString();
244+
242245
return $this->setTrace($throwable->getTrace(), $throwable->getFile(), $throwable->getLine());
243246
}
244247

@@ -324,4 +327,27 @@ private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value)
324327

325328
return $array['__PHP_Incomplete_Class_Name'];
326329
}
330+
331+
public function getTraceAsString()
332+
{
333+
return $this->traceAsString;
334+
}
335+
336+
public function __toString()
337+
{
338+
$message = '';
339+
340+
foreach (array_merge(array($this), $this->getAllPrevious()) as $exception) {
341+
$message .= $exception->getClass();
342+
343+
if ('' != $exception->getMessage()) {
344+
$message .= ': '.$exception->getMessage();
345+
}
346+
347+
$message .= ' in '.$exception->getFile().':'.$exception->getLine().
348+
"\nStack trace:\n".$exception->getTraceAsString()."\n";
349+
}
350+
351+
return rtrim($message);
352+
}
327353
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,37 @@ public function testAnonymousClass()
346346
$this->assertSame('Class "RuntimeException@anonymous" blah.', $flattened->getMessage());
347347
}
348348

349+
public function testTraceAsStringEmptyMessage()
350+
{
351+
$exception = new \RuntimeException();
352+
353+
$flattened = FlattenException::create($exception);
354+
355+
$this->assertSame($exception->getTraceAsString(), $flattened->getTraceAsString());
356+
}
357+
358+
public function testTraceAsString()
359+
{
360+
$test = function ($a, $b, $c, $d) {
361+
return new \RuntimeException('This is a test message');
362+
};
363+
364+
$exception = $test('foo123', 1, null, 1.5);
365+
366+
$flattened = FlattenException::create($exception);
367+
368+
$this->assertSame($exception->getTraceAsString(), $flattened->getTraceAsString());
369+
}
370+
371+
public function testToString()
372+
{
373+
$exception = new \RuntimeException('This is a test message');
374+
375+
$flattened = FlattenException::create($exception);
376+
377+
$this->assertSame($exception->__toString(), $flattened->__toString());
378+
}
379+
349380
private function createException($foo)
350381
{
351382
return new \Exception();

0 commit comments

Comments
 (0)