Last active
September 6, 2024 18:45
-
-
Save Thinkscape/805ba8b91cdce6bcaf7c to your computer and use it in GitHub Desktop.
Make any PHP Exception serializable by flattening complex values in backtrace.
This file contains hidden or 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 | |
function flattenExceptionBacktrace(\Exception $exception) { | |
$traceProperty = (new \ReflectionClass('Exception'))->getProperty('trace'); | |
$traceProperty->setAccessible(true); | |
$flatten = function(&$value, $key) { | |
if ($value instanceof \Closure) { | |
$closureReflection = new \ReflectionFunction($value); | |
$value = sprintf( | |
'(Closure at %s:%s)', | |
$closureReflection->getFileName(), | |
$closureReflection->getStartLine() | |
); | |
} elseif (is_object($value)) { | |
$value = sprintf('object(%s)', get_class($value)); | |
} elseif (is_resource($value)) { | |
$value = sprintf('resource(%s)', get_resource_type($value)); | |
} | |
}; | |
do { | |
$trace = $traceProperty->getValue($exception); | |
foreach($trace as &$call) { | |
array_walk_recursive($call['args'], $flatten); | |
} | |
$traceProperty->setValue($exception, $trace); | |
} while($exception = $exception->getPrevious()); | |
$traceProperty->setAccessible(false); | |
} |
Be aware to use this function.
As it changes real method arguments passed by reference.
function foo(&$bar)
{
flattenExceptionBacktrace(new \Exception());
}
$bar = new \DateTime();
foo($bar);
var_dump($bar); // outputs: string(16) "object(DateTime)"
I did following to convert an exception into an array to be logged as json. Here I didn't care arguments
public function exceptiontoArray(null|Exception $exception)
{
if($exception) {
return [
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'trace' => $exception->getTrace(),
'previous' => exceptiontoArray($exception->getPrevious())
];
} else {
return [];
}
}
I use ReflectionObject to serialize exception, this dropped non-scalar properties, be careful.
function object2ScalarArray(object $object)
{
$ref = new \ReflectionObject($object);
$props = [];
foreach ($ref->getProperties() as $prop) {
$prop->setAccessible(true);
$val = $prop->getValue($object);
if (is_scalar($val)) {
$props[$prop->getName()] = $val;
}
}
return [
'class' => $object::class,
'props' => $props
];
}
function scalarArray2Object(array $scalarArray)
{
$ref = new \ReflectionClass($scalarArray['class']);
$object = $ref->newInstanceWithoutConstructor();
$ref = new \ReflectionObject($object);
foreach ($scalarArray['props'] as $name => $val) {
$prop = $ref->getProperty($name);
$prop->setAccessible(true);
$prop->setValue($object, $val);
}
return $object;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have found that with the way that PHP 7 handles errors now, treating them as exceptions, my error handlers receive Error type throwables too. As per PHP's reccomendation, we must now expect to receive Throwables, rather than just Exceptions.
Unfortunately, while we can change the type hint to throwable, we cannot just change the reflection class type to Throwable as it does not contain the property trace, but if we check if the throwable is an instance of Error or Exception, we can then get the trace property as expected.
If we do not do this, then any throwable type errors sent to this function will raise an exception that they are not an instance of the object type exception (in a more cryptic manner). For the PHP 7 ready modifications, see my fork.