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 67083b6

Browse filesBrowse files
committed
feature #28879 [Debug] Mimic __toString php behavior in FlattenException (Deltachaos)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Debug] Mimic __toString php behavior in FlattenException | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | symfony/symfony-docs# The `Symfony\Component\Debug\Exception\FlattenException` object is returned by `Symfony\Component\HttpKernel\DataCollector\ExceptionDataCollector::getException` method, but the docblock of this method indicates that it should return `\Exception` object. As the `FlattenException` class should behave as much as possible like a php `\Exception` object, it should implement the same methods as `\Exception`. This PR is adding `__toString` and `getTraceAsString` methods. Those methods are (in my opinion) the most useful methods of a `\Exception` object. A potential use case (where i am stumbled across this inconsistency) is to get the last exception of a request in a `WebTestCase` using the profiler and printing the trace. Commits ------- 514a1b5 [Debug] Mimic __toString php behavior in FlattenException
2 parents 47242e3 + 514a1b5 commit 67083b6
Copy full SHA for 67083b6

File tree

Expand file treeCollapse file tree

3 files changed

+70
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+70
-0
lines changed

‎src/Symfony/Component/Debug/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Debug/CHANGELOG.md
+3Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Debug/Exception/FlattenException.php
+32Lines 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([$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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Debug/Tests/Exception/FlattenExceptionTest.php
+35Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.