diff --git a/src/Symfony/Component/Debug/CHANGELOG.md b/src/Symfony/Component/Debug/CHANGELOG.md index 2b7bfc8ba30ed..367e834f01e7e 100644 --- a/src/Symfony/Component/Debug/CHANGELOG.md +++ b/src/Symfony/Component/Debug/CHANGELOG.md @@ -5,6 +5,9 @@ CHANGELOG ----- * made the `ErrorHandler` and `ExceptionHandler` classes final +* added `Exception\FlattenException::getAsString` and +`Exception\FlattenException::getTraceAsString` to increase compatibility to php +exception objects 4.0.0 ----- diff --git a/src/Symfony/Component/Debug/Exception/FlattenException.php b/src/Symfony/Component/Debug/Exception/FlattenException.php index d016bb2fb45cc..304df0405c267 100644 --- a/src/Symfony/Component/Debug/Exception/FlattenException.php +++ b/src/Symfony/Component/Debug/Exception/FlattenException.php @@ -27,6 +27,7 @@ class FlattenException private $code; private $previous; private $trace; + private $traceAsString; private $class; private $statusCode; private $headers; @@ -239,6 +240,8 @@ public function setTraceFromException(\Exception $exception) public function setTraceFromThrowable(\Throwable $throwable) { + $this->traceAsString = $throwable->getTraceAsString(); + return $this->setTrace($throwable->getTrace(), $throwable->getFile(), $throwable->getLine()); } @@ -324,4 +327,33 @@ private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value) return $array['__PHP_Incomplete_Class_Name']; } + + public function getTraceAsString() + { + return $this->traceAsString; + } + + public function getAsString() + { + $message = ''; + $next = false; + + foreach (array_reverse(array_merge([$this], $this->getAllPrevious())) as $exception) { + if ($next) { + $message .= 'Next '; + } else { + $next = true; + } + $message .= $exception->getClass(); + + if ('' != $exception->getMessage()) { + $message .= ': '.$exception->getMessage(); + } + + $message .= ' in '.$exception->getFile().':'.$exception->getLine(). + "\nStack trace:\n".$exception->getTraceAsString()."\n\n"; + } + + return rtrim($message); + } } diff --git a/src/Symfony/Component/Debug/Tests/Exception/FlattenExceptionTest.php b/src/Symfony/Component/Debug/Tests/Exception/FlattenExceptionTest.php index eb884b51c1017..e86210b903b02 100644 --- a/src/Symfony/Component/Debug/Tests/Exception/FlattenExceptionTest.php +++ b/src/Symfony/Component/Debug/Tests/Exception/FlattenExceptionTest.php @@ -346,6 +346,41 @@ public function testAnonymousClass() $this->assertSame('Class "RuntimeException@anonymous" blah.', $flattened->getMessage()); } + public function testToStringEmptyMessage() + { + $exception = new \RuntimeException(); + + $flattened = FlattenException::create($exception); + + $this->assertSame($exception->getTraceAsString(), $flattened->getTraceAsString()); + $this->assertSame($exception->__toString(), $flattened->getAsString()); + } + + public function testToString() + { + $test = function ($a, $b, $c, $d) { + return new \RuntimeException('This is a test message'); + }; + + $exception = $test('foo123', 1, null, 1.5); + + $flattened = FlattenException::create($exception); + + $this->assertSame($exception->getTraceAsString(), $flattened->getTraceAsString()); + $this->assertSame($exception->__toString(), $flattened->getAsString()); + } + + public function testToStringParent() + { + $exception = new \LogicException('This is message 1'); + $exception = new \RuntimeException('This is messsage 2', 500, $exception); + + $flattened = FlattenException::create($exception); + + $this->assertSame($exception->getTraceAsString(), $flattened->getTraceAsString()); + $this->assertSame($exception->__toString(), $flattened->getAsString()); + } + private function createException($foo) { return new \Exception(); 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