Skip to content

Instantly share code, notes, and snippets.

@pgorod
Forked from abtris/exception.php
Created June 23, 2019 22:24
Show Gist options
  • Save pgorod/9806be54301a56d7da3d713119c94fe5 to your computer and use it in GitHub Desktop.
Save pgorod/9806be54301a56d7da3d713119c94fe5 to your computer and use it in GitHub Desktop.
<?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();
echo getExceptionTraceAsString($e);
}
}
class Testing
{
public function pokus($string)
{
$this->testException($string);
}
public static function pokus2($string)
{
self::testException($string);
}
public function testException($string = "fdfsdfsdfdfsd")
{
try {
throw new Exception('chyba' . $string);
} catch (Exception $e) {
print_r($e->getTrace());
echo $e->getTraceAsString();
echo getExceptionTraceAsString($e);
}
}
}
function getExceptionTraceAsString($exception) {
$rtn = "";
$count = 0;
foreach ($exception->getTrace() as $frame) {
$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);
}
$rtn .= sprintf( "#%s %s(%s): %s(%s)\n",
$count,
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 );
$count++;
}
return $rtn;
}
$test = new Testing();
$test->pokus("sdfdskflnsdlkfnsdlkfnlksdnflknsdlkfnklsdnfkl");
Testing::pokus2('fsfdsfsdfsdf');
tttt("fdfdfd");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment