Skip to content

Commit

Permalink
Fix autoloader throwing an exception while trying to serialize a poss…
Browse files Browse the repository at this point in the history
…ible `callable` (#1280)
  • Loading branch information
Jean85 authored Jan 15, 2022
1 parent 1b1c35a commit e4dd045
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Fix another case of serialization with autoloader that throws exceptions (#1280)

## 3.3.6 (2022-01-14)

- Optimize `Span` constructor and add benchmarks (#1274)
Expand Down
8 changes: 6 additions & 2 deletions src/Serializer/AbstractSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,12 @@ protected function serializeValue($value)
return 'Resource ' . get_resource_type($value);
}

if (\is_callable($value)) {
return $this->serializeCallable($value);
try {
if (\is_callable($value)) {
return $this->serializeCallable($value);
}
} catch (\Throwable $exception) {
// Do nothing on purpose
}

if (\is_array($value)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/phpt/serialize_broken_class.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ testSerialization([BrokenClass::class, 'brokenMethod']);

?>
--EXPECT--
"Sentry\\Tests\\Fixtures\\code\\BrokenClass::brokenMethod {serialization error}"
"Sentry\\Tests\\Fixtures\\code\\BrokenClass::brokenMethod"
["Sentry\\Tests\\Fixtures\\code\\BrokenClass","brokenMethod"]
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ while (!file_exists($vendor . '/vendor')) {

require $vendor . '/vendor/autoload.php';

function testSerialization() {
$serializer = new Serializer(new Options());
function testSerialization(int $depth = 3) {
$serializer = new Serializer(new Options(), $depth);

echo json_encode($serializer->serialize(['FakeClass', 'fakeMethod']));
echo PHP_EOL;
}

testSerialization();
echo PHP_EOL;
$brokenAutoloader = function (string $classname): void {
throw new \RuntimeException('Autoloader throws while loading ' . $classname);
};
Expand All @@ -36,10 +36,12 @@ spl_autoload_register($brokenAutoloader, true, true);

try {
testSerialization();
testSerialization(0);
} finally {
spl_autoload_unregister($brokenAutoloader);
}
?>
--EXPECT--
["FakeClass","fakeMethod"]
["FakeClass","fakeMethod"]
"Array of length 2"

0 comments on commit e4dd045

Please sign in to comment.