Skip to content

Commit

Permalink
Merge pull request #12 from netlogix/bugfix/use-compound-for-production
Browse files Browse the repository at this point in the history
BUGFIX: Only use CompoundStorage for Production Context
  • Loading branch information
saschanowak authored Feb 4, 2021
2 parents 4789273 + ed4f7a8 commit 6eee760
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Classes/Scope/Release/PathPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
21 changes: 17 additions & 4 deletions Classes/ThrowableStorage/CompoundStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -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;
}

}
23 changes: 23 additions & 0 deletions Tests/Functional/ThrowableStorage/CompoundStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}

0 comments on commit 6eee760

Please sign in to comment.