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

Skip to content

Commit 0323df8

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

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-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: 32 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,33 @@ 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+
$next = false;
340+
341+
foreach (array_reverse(array_merge(array($this), $this->getAllPrevious())) as $exception) {
342+
if ($next) {
343+
$message .= 'Next ';
344+
} else {
345+
$next = true;
346+
}
347+
$message .= $exception->getClass();
348+
349+
if ('' != $exception->getMessage()) {
350+
$message .= ': '.$exception->getMessage();
351+
}
352+
353+
$message .= ' in '.$exception->getFile().':'.$exception->getLine().
354+
"\nStack trace:\n".$exception->getTraceAsString()."\n\n";
355+
}
356+
357+
return rtrim($message);
358+
}
327359
}

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

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

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

0 commit comments

Comments
 (0)