Repository: getsentry/sentry-php (php)

Issue:
Sentry trace Serializer issue
sentry/sentry  sdk 2.4.1

Hi guys,

We create a magento sentry integration. It works well, except in this scenario. 

It requires Magento 2 knowledge

In sentry I see issue with such a name
```Cannot declare class Vendor\Customer\Plugin\Api\CustomerRepositoryPlugin, because the name is already in use```

Inside issue in the message there is a actual exception message. No real Exception trace, just trace about class redeclare.

```
 in include called at /var/www/magento/releases/20201001162424/vendor/composer/ClassLoader.php (444)
 in Composer\Autoload\includeFile called at /var/www/magento/releases/20201001162424/vendor/composer/ClassLoader.php (322)
 in Composer\Autoload\ClassLoader::loadClass called at ? (?)
 in spl_autoload_call called at ? (?)
 in is_callable called at /var/www/magento/releases/20201001162424/vendor/sentry/sentry/src/Serializer/AbstractSerializer.php (102)
 in Sentry\Serializer\AbstractSerializer::serializeRecursively called at /var/www/magento/releases/20201001162424/vendor/sentry/sentry/src/Serializer/AbstractSerializer.php (110)
 in Sentry\Serializer\AbstractSerializer::serializeRecursively called at /var/www/magento/releases/20201001162424/vendor/sentry/sentry/src/Serializer/RepresentationSerializer.php (21)
 in Sentry\Serializer\RepresentationSerializer::representationSerialize called at /var/www/magento/releases/20201001162424/vendor/sentry/sentry/src/Stacktrace.php (177)
 in Sentry\Stacktrace::addFrame called at /var/www/magento/releases/20201001162424/vendor/sentry/sentry/src/Stacktrace.php (102)
 in Sentry\Stacktrace::createFromBacktrace called at /var/www/magento/releases/20201001162424/vendor/sentry/sentry/src/EventFactory.php (149)
 in Sentry\EventFactory::addThrowableToEvent called at /var/www/magento/releases/20201001162424/vendor/sentry/sentry/src/EventFactory.php (110)
 in Sentry\EventFactory::create called at /var/www/magento/releases/20201001162424/vendor/sentry/sentry/src/Client.php (183)
 in Sentry\Client::prepareEvent called at /var/www/magento/releases/20201001162424/vendor/sentry/sentry/src/Client.php (118)
 in Sentry\Client::captureEvent called at /var/www/magento/releases/20201001162424/vendor/sentry/sentry/src/Client.php (110)
 in Sentry\Client::captureException called at /var/www/magento/releases/20201001162424/vendor/sentry/sentry/src/State/Hub.php (132)
 in Sentry\State\Hub::captureException called at /var/www/magento/releases/20201001162424/vendor/mygento/module-sentry/Model/SentryHandler.php (285)
 in Mygento\Sentry\Model\SentryHandler::Mygento\Sentry\Model\{closure} called at /var/www/magento/releases/20201001162424/vendor/sentry/sentry/src/State/Hub.php (87)
 in Sentry\State\Hub::withScope called at /var/www/magento/releases/20201001162424/vendor/mygento/module-sentry/Model/SentryHandler.php (289)
 in Mygento\Sentry\Model\SentryHandler::write called at /var/www/magento/releases/20201001162424/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php (39)
 in Monolog\Handler\AbstractProcessingHandler::handle called at /var/www/magento/releases/20201001162424/vendor/monolog/monolog/src/Monolog/Logger.php (344)

```

AbstractSerializer.php somehow recreates plugin class

Pull request:
Serialization of an array of 2 strings that looks like a callable (again)
Fixes #1112, again

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dd32ad8..ea590c1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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)
diff --git a/src/Serializer/AbstractSerializer.php b/src/Serializer/AbstractSerializer.php
index 2e7ffae..2da56cf 100644
--- a/src/Serializer/AbstractSerializer.php
+++ b/src/Serializer/AbstractSerializer.php
@@ -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)) {


Test patch:
diff --git a/tests/phpt/serialize_broken_class.phpt b/tests/phpt/serialize_broken_class.phpt
index 5511595..0df9ab8 100644
--- a/tests/phpt/serialize_broken_class.phpt
+++ b/tests/phpt/serialize_broken_class.phpt
@@ -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"]
diff --git a/tests/phpt/serialize_callable_that_makes_autoloader_throw.phpt b/tests/phpt/serialize_callable_that_makes_autoloader_throw.phpt
index dc1bd62..57f31ba 100644
--- a/tests/phpt/serialize_callable_that_makes_autoloader_throw.phpt
+++ b/tests/phpt/serialize_callable_that_makes_autoloader_throw.phpt
@@ -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);
 };
@@ -36,6 +36,7 @@ spl_autoload_register($brokenAutoloader, true, true);
 
 try {
     testSerialization();
+    testSerialization(0);
 } finally {
     spl_autoload_unregister($brokenAutoloader);
 }
@@ -43,3 +44,4 @@ try {
 --EXPECT--
 ["FakeClass","fakeMethod"]
 ["FakeClass","fakeMethod"]
+"Array of length 2"