-
-
Save davidfuhr/ab338a6ad252ad02255f to your computer and use it in GitHub Desktop.
Better getExceptionTraceAsString - http://stackoverflow.com/questions/1949345/how-can-i-get-the-full-string-of-phps-gettraceasstring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Pokus; | |
class Exception extends \Exception | |
{ | |
} | |
function tttt($string) | |
{ | |
try { | |
throw new Exception('chyba' . $string); | |
} catch (Exception $e) { | |
print_r($e->getTrace()); | |
echo $e->getTraceAsString(), "\n"; | |
echo getFullTraceAsString($e); | |
} | |
} | |
class Testing | |
{ | |
public function pokus($string) | |
{ | |
$this->testException($string); | |
} | |
public static function pokus2($string) | |
{ | |
self::testStaticException($string); | |
} | |
public static function testStaticException($string = "fdfsdfsdfdfsd") | |
{ | |
try { | |
throw new Exception('chyba' . $string); | |
} catch (Exception $e) { | |
print_r($e->getTrace()); | |
echo $e->getTraceAsString(), "\n"; | |
echo getFullTraceAsString($e); | |
} | |
} | |
public function testException($string = "fdfsdfsdfdfsd") | |
{ | |
try { | |
throw new Exception('chyba' . $string); | |
} catch (Exception $e) { | |
print_r($e->getTrace()); | |
echo $e->getTraceAsString(), "\n"; | |
echo getFullTraceAsString($e); | |
echo $e; | |
} | |
} | |
public function testNestedException($foo = 'bar') | |
{ | |
try { | |
try { | |
$this->throwException($foo); | |
} | |
catch (\Exception $e) { | |
throw new \UnexpectedValueException('xoxo', null, $e); | |
} | |
} | |
catch (\Exception $e) { | |
print_r($e->getTrace()); | |
echo $e->getTraceAsString(), "\n"; | |
echo getFullTraceAsString($e); | |
echo $e; | |
} | |
} | |
public function throwException($foo = 'bar') | |
{ | |
throw new Exception($foo); | |
} | |
} | |
/** | |
* Returns the trace including all previous exceptions as string | |
* | |
* @param \Exception $exception | |
* | |
* @return string | |
*/ | |
function getFullTraceAsString(\Exception $exception) | |
{ | |
$exceptionStack = array($exception); | |
while ($exception = $exception->getPrevious()) { | |
array_push($exceptionStack, $exception); | |
} | |
$traceAsString = ''; | |
$frameCount = 0; | |
while ($exception = array_pop($exceptionStack)) { | |
// we need to skip the first frame of previous exceptions because the | |
// last frame of the current exception is the same as the first frame | |
// of the previous exception | |
$skipNextFrame = true; | |
if (0 === $frameCount) { | |
$skipNextFrame = false; | |
} | |
foreach ($exception->getTrace() as $frame) { | |
if ($skipNextFrame) { | |
$skipNextFrame = false; | |
continue; | |
} | |
$args = ""; | |
if (isset($frame['args'])) { | |
$args = array(); | |
foreach ($frame['args'] as $arg) { | |
if (is_string($arg)) { | |
$args[] = "'" . $arg . "'"; | |
} elseif (is_array($arg)) { | |
$args[] = 'Array'; | |
} elseif (is_null($arg)) { | |
$args[] = 'NULL'; | |
} elseif (is_bool($arg)) { | |
$args[] = ($arg) ? 'true' : 'false'; | |
} elseif (is_object($arg)) { | |
$args[] = get_class($arg); | |
} elseif (is_resource($arg)) { | |
$args[] = get_resource_type($arg); | |
} else { | |
$args[] = $arg; | |
} | |
} | |
$args = join(', ', $args); | |
} | |
$traceAsString .= sprintf( | |
"#%s %s(%s): %s(%s)\n", | |
$frameCount, | |
isset($frame['file']) ? $frame['file'] : 'unknown file', | |
isset($frame['line']) ? $frame['line'] : 'unknown line', | |
(isset($frame['class'])) ? $frame['class'] . $frame['type'] . $frame['function'] : $frame['function'], | |
$args | |
); | |
$frameCount++; | |
} | |
} | |
$traceAsString .= "#$frameCount {main}\n"; | |
return $traceAsString; | |
} | |
$test = new Testing(); | |
$test->pokus("sdfdskflnsdlkfnsdlkfnlksdnflknsdlkfnklsdnfkl"); | |
$test->testNestedException('NESTED'); | |
Testing::pokus2('fsfdsfsdfsdf'); | |
tttt("fdfdfd"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment