Skip to content

Commit ff2f7d2

Browse files
committed
[Debug] Mimic __toString php behavior in FlattenException
1 parent 5a3e894 commit ff2f7d2

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/Symfony/Component/Debug/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ CHANGELOG
55
-----
66

77
* made the `ErrorHandler` and `ExceptionHandler` classes final
8+
* added `Exception\FlattenException::getAsString` and
9+
`Exception\FlattenException::getTraceAsString` to increase compatibility to php
10+
exception objects
811

912
4.0.0
1013
-----

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 getAsString()
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->getAsString());
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->getAsString());
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->getAsString());
382+
}
383+
349384
private function createException($foo)
350385
{
351386
return new \Exception();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy