Skip to content

Commit e3228b5

Browse files
committed
fix(cache): name lazy getItem failures clearly
1 parent 5f69f1d commit e3228b5

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎src/Adapter/Common/AbstractCachePool.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function getItem(string $key): PhpCacheItem
107107
try {
108108
return $this->fetchObjectFromCache($key);
109109
} catch (\Exception $e) {
110-
$this->handleException($e, __FUNCTION__);
110+
$this->handleException($e, 'getItem');
111111
}
112112
};
113113

‎src/Adapter/PHPArray/Tests/ArrayCachePoolTest.php‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ public function testStorageExceptionIsWrappedWhenSaving()
7676
}
7777
}
7878

79+
public function testStorageExceptionUsesGetItemOperationName()
80+
{
81+
$exception = new \RuntimeException('backend down');
82+
$pool = new ThrowingArrayCachePool();
83+
$pool->fetchException = $exception;
84+
85+
try {
86+
$pool->getItem('key')->isHit();
87+
self::fail('getItem did not wrap the storage exception');
88+
} catch (CachePoolException $wrapped) {
89+
self::assertSame($exception, $wrapped->getPrevious());
90+
self::assertSame('Exception thrown when executing "getItem". ', $wrapped->getMessage());
91+
}
92+
}
93+
7994
public function testSetMultipleWrapsItemExpirationFailure()
8095
{
8196
$exception = new InvalidArgumentException('invalid');
@@ -367,13 +382,24 @@ final class ThrowingArrayCachePool extends ArrayCachePool
367382
{
368383
public ?\RuntimeException $clearException = null;
369384

385+
public ?\RuntimeException $fetchException = null;
386+
370387
public ?\RuntimeException $saveException = null;
371388

372389
protected function clearAllObjectsFromCache(): bool
373390
{
374391
throw $this->clearException ?? new \LogicException('clearException is not configured');
375392
}
376393

394+
protected function fetchObjectFromCache(string $key): array
395+
{
396+
if (null !== $this->fetchException) {
397+
throw $this->fetchException;
398+
}
399+
400+
return parent::fetchObjectFromCache($key);
401+
}
402+
377403
protected function storeItemInCache(PhpCacheItem $item, ?int $ttl): bool
378404
{
379405
throw $this->saveException ?? new \LogicException('saveException is not configured');

0 commit comments

Comments
 (0)