Skip to content

Instantly share code, notes, and snippets.

@fbrnc
Last active December 17, 2015 04:08
Show Gist options
  • Save fbrnc/5548318 to your computer and use it in GitHub Desktop.
Save fbrnc/5548318 to your computer and use it in GitHub Desktop.
Advanced exception stack trace with untruncated parameters and remote call links
<?php
final class Mage {
/**
* Get advanced exception trace
*
* @param Exception $exception
* @return string
*/
public static function getExceptionTraceAsString(Exception $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);
}
// create remote call links
if (!empty($frame['file']) && !empty($frame['line'])) {
$lineAndFile = sprintf('%s(%s)', $frame['file'], $frame['line']);
$url = sprintf('http://localhost:8091/?message=%s:%s', $frame['file'], $frame['line']);
$lineAndFile = sprintf('<a href="%s" onclick="var ajax = new XMLHttpRequest(); ajax.open(\'GET\', this.href); ajax.send(null); return false">%s</a>',
$url,
$lineAndFile
);
} else {
$lineAndFile = '[UNKOWN FILE AND LINE]';
}
$rtn .= sprintf("#%s %s: %s(%s)\n",
$count,
$lineAndFile,
$frame['function'],
$args);
$count++;
}
return $rtn;
}
public static function printException(Exception $e, $extra = '') {
if (self::$_isDeveloperMode) {
print '<pre>';
if (!empty($extra)) {
print $extra . "\n\n";
}
print $e->getMessage() . "\n\n";
// -------------- change following lines: -------------------
// print $e->getTraceAsString();
print self::getExceptionTraceAsString($e);
print '</pre>';
} else {
...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment