Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nh-mike/fde9f69a57bc45c5b491d90fb2ee08df to your computer and use it in GitHub Desktop.
Save nh-mike/fde9f69a57bc45c5b491d90fb2ee08df to your computer and use it in GitHub Desktop.
Make any PHP Exception serializable by flattening complex values in backtrace.
protected static function flattenExceptionBacktrace(Exception $exception) {
$traceProperty = (new \ReflectionClass('Exception'))->getProperty('trace');
$traceProperty->setAccessible(true);
do {
$trace = $traceProperty->getValue($exception);
foreach($trace as &$call) {
foreach($call['args'] as $key => $value) {
if ($value instanceof \Closure) {
$call['args'][$key] = '(Closure)';
} elseif (is_object($value) && !method_exists($value, '__toString')) {
$call['args'][$key] = sprintf('object(%s)', get_class($value));
} elseif (is_resource($value)) {
$call['args'][$key] = sprintf('resource(%s)', get_resource_type($value));
}
}
}
$traceProperty->setValue($exception, $trace);
} while($exception = $exception->getPrevious());
$traceProperty->setAccessible(false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment