Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7ef80ff

Browse files
minor #31395 [Cache] Log a more readable message when trying to cache an unsupported type (Deuchnord)
This PR was merged into the 4.3 branch. Discussion ---------- [Cache] Log a more readable message when trying to cache an unsupported type | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #29710 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | Improved the warning risen when trying to save something that has a non-supported type in the Simple cache. For instance, let's say the following code: ```php class TestCommand extends Command { private $cache; public function __construct(CacheInterface $cache) { parent::__construct(); $this->cache = $cache; } // ... protected function execute(InputInterface $input, OutputInterface $output) { $n = $this->cache->get('n', function () { return function () { return rand(0,10); }; }); dump($n()); } } ``` Running this code will give the following: ``` 14:32:03 WARNING [cache] Could not save key "n" in cache: the Closure type is not supported. 0 ``` Commits ------- 21ba3c0 [Cache] Log a more readable error message when saving into cache fails
2 parents 8335810 + 21ba3c0 commit 7ef80ff

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

src/Symfony/Component/Cache/Adapter/AbstractAdapter.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ public function commit()
164164
$ok = false;
165165
$v = $values[$id];
166166
$type = \is_object($v) ? \get_class($v) : \gettype($v);
167-
CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', ['key' => substr($id, \strlen($this->namespace)), 'type' => $type, 'exception' => $e instanceof \Exception ? $e : null]);
167+
$message = sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.');
168+
CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null]);
168169
}
169170
} else {
170171
foreach ($values as $id => $v) {
@@ -186,7 +187,8 @@ public function commit()
186187
}
187188
$ok = false;
188189
$type = \is_object($v) ? \get_class($v) : \gettype($v);
189-
CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', ['key' => substr($id, \strlen($this->namespace)), 'type' => $type, 'exception' => $e instanceof \Exception ? $e : null]);
190+
$message = sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.');
191+
CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null]);
190192
}
191193
}
192194

src/Symfony/Component/Cache/Adapter/AbstractTagAwareAdapter.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ public function commit()
178178
$ok = false;
179179
$v = $values[$id];
180180
$type = \is_object($v) ? \get_class($v) : \gettype($v);
181-
CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', ['key' => substr($id, \strlen($this->namespace)), 'type' => $type, 'exception' => $e instanceof \Exception ? $e : null]);
181+
$message = sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.');
182+
CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null]);
182183
}
183184
} else {
184185
foreach ($values as $id => $v) {
@@ -201,7 +202,8 @@ public function commit()
201202
}
202203
$ok = false;
203204
$type = \is_object($v) ? \get_class($v) : \gettype($v);
204-
CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', ['key' => substr($id, \strlen($this->namespace)), 'type' => $type, 'exception' => $e instanceof \Exception ? $e : null]);
205+
$message = sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.');
206+
CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null]);
205207
}
206208
}
207209

src/Symfony/Component/Cache/Traits/ArrayTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ private function freeze($value, $key)
128128
$serialized = serialize($value);
129129
} catch (\Exception $e) {
130130
$type = \is_object($value) ? \get_class($value) : \gettype($value);
131-
CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', ['key' => $key, 'type' => $type, 'exception' => $e]);
131+
$message = sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.');
132+
CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null]);
132133

133134
return;
134135
}

0 commit comments

Comments
 (0)