From b577ae8b87733f0273d7e00a3691427d2c18c342 Mon Sep 17 00:00:00 2001 From: Lars Lauger Date: Thu, 4 Feb 2021 11:50:28 +0100 Subject: [PATCH 1/3] BUGFIX: Throw logged exception if Bootstrap::$staticObjectManager is unset If an exception is throws during a compile run, the objectManager won't be initialized yet. --- Classes/ThrowableStorage/CompoundStorage.php | 21 +++++++++++++---- .../ThrowableStorage/CompoundStorageTest.php | 23 +++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/Classes/ThrowableStorage/CompoundStorage.php b/Classes/ThrowableStorage/CompoundStorage.php index 763cfa4..b1f401e 100644 --- a/Classes/ThrowableStorage/CompoundStorage.php +++ b/Classes/ThrowableStorage/CompoundStorage.php @@ -83,7 +83,10 @@ private function __construct(string $primaryStorageClassName, string ... $additi public function logThrowable(\Throwable $throwable, array $additionalData = []) { - $this->initializeStorages(); + if (!$this->initializeStorages()) { + // could not initialize storages, throw exception + throw $throwable; + } $message = $this->primaryStorage->logThrowable($throwable, $additionalData); @@ -106,6 +109,10 @@ public function setBacktraceRenderer(\Closure $backtraceRenderer) private function createStorage(string $storageClassName): ThrowableStorageInterface { + if (!Bootstrap::$staticObjectManager) { + throw new \RuntimeException('Bootstrap::$staticObjectManager is not set yet', 1612434395); + } + assert(is_a($storageClassName, ThrowableStorageInterface::class, true)); $bootstrap = Bootstrap::$staticObjectManager->get(Bootstrap::class); $configurationManager = $bootstrap->getEarlyInstance(ConfigurationManager::class); @@ -123,15 +130,21 @@ private function createStorage(string $storageClassName): ThrowableStorageInterf return $storage; } - private function initializeStorages(): void + private function initializeStorages(): bool { if ($this->initialized) { - return; + return true; } - ($this->initializeStoragesClosure)(); + try { + ($this->initializeStoragesClosure)(); + } catch (\Throwable $t) { + return false; + } $this->initialized = true; + + return true; } } diff --git a/Tests/Functional/ThrowableStorage/CompoundStorageTest.php b/Tests/Functional/ThrowableStorage/CompoundStorageTest.php index 82cf429..dd2e278 100644 --- a/Tests/Functional/ThrowableStorage/CompoundStorageTest.php +++ b/Tests/Functional/ThrowableStorage/CompoundStorageTest.php @@ -3,6 +3,7 @@ namespace Netlogix\Sentry\Tests\Functional\ThrowableStorage; +use Neos\Flow\Core\Bootstrap; use Neos\Flow\Tests\FunctionalTestCase; use Netlogix\Sentry\Exception\Test; use Netlogix\Sentry\ThrowableStorage\CompoundStorage; @@ -71,4 +72,26 @@ public function RequestInformationRenderer_and_BacktraceRenderer_are_passed_to_a self::assertSame($backtraceRenderer, TestThrowableStorage2::$backtraceRenderer); } + /** + * @test + */ + public function When_Bootstrap_staticObjectManager_is_unset_the_logged_exception_is_thrown(): void + { + $storage = CompoundStorage::createWithOptions([ + 'storages' => [ + TestThrowableStorage1::class, + TestThrowableStorage2::class, + ] + ]); + + Bootstrap::$staticObjectManager = null; + + $throwable = new Test('foo', 1); + + self::expectException(Test::class); + self::expectExceptionCode(1); + + $storage->logThrowable($throwable); + } + } From 505b02290aa6d30189edbbbad2b36c06f54916d3 Mon Sep 17 00:00:00 2001 From: Lars Lauger Date: Thu, 4 Feb 2021 11:51:25 +0100 Subject: [PATCH 2/3] BUGFIX: Don't use CompoundStorage in Development Context If an exception is thrown during a compile run, the CompoundStorage will not be able to create the configured storages. --- Configuration/{ => Production}/Settings.ThrowableStorage.yaml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Configuration/{ => Production}/Settings.ThrowableStorage.yaml (100%) diff --git a/Configuration/Settings.ThrowableStorage.yaml b/Configuration/Production/Settings.ThrowableStorage.yaml similarity index 100% rename from Configuration/Settings.ThrowableStorage.yaml rename to Configuration/Production/Settings.ThrowableStorage.yaml From ed4f7a8876a9be4043848fd6ccd57dba6efc0f54 Mon Sep 17 00:00:00 2001 From: Lars Lauger Date: Thu, 4 Feb 2021 12:16:57 +0100 Subject: [PATCH 3/3] BUGFIX: Suppress warnings in PathPattern --- Classes/Scope/Release/PathPattern.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/Scope/Release/PathPattern.php b/Classes/Scope/Release/PathPattern.php index 89f9dd5..2dc2c6f 100644 --- a/Classes/Scope/Release/PathPattern.php +++ b/Classes/Scope/Release/PathPattern.php @@ -24,7 +24,7 @@ final class PathPattern implements ReleaseProvider public function getRelease(): ?string { $path = trim(realpath($this->pathToMatch), '/'); - if (preg_match($this->pathPattern, $path, $matches) === 1) { + if (@preg_match($this->pathPattern, $path, $matches) === 1) { return $matches[1]; }