fix: wrap cache purge operations in try-catch for graceful degradation#857
fix: wrap cache purge operations in try-catch for graceful degradation#857
Conversation
Cache purge failures should not propagate exceptions up the stack and cause HTTP request failures. This matches the existing pattern for cache load and save operations which already use try-catch with warning logging. When cache is slow or unavailable, purge operations now: - Log a warning message instead of throwing - Allow the request to continue successfully - Prevent cascading failures during cache infrastructure issues Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
📝 WalkthroughWalkthroughThe Database class now wraps cache purging operations in Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~5 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Greptile SummaryWraps Confidence Score: 5/5Safe to merge — minimal, focused change with no logic risk. Both new catch blocks are consistent with the established pattern for cache load/save. No P0/P1 issues; the only finding is a stale @throws docblock. No files require special attention. Important Files Changed
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/Database/Database.php (1)
8217-8226: Best-effort purge currently aborts on first failure.At Line 8219-Line 8223, one thrown exception stops the rest of the purge path, so remaining keys may stay stale. Splitting try/catch blocks per operation keeps graceful degradation while maximizing invalidation coverage.
♻️ Suggested refactor
- try { - $documentKeys = $this->cache->list($collectionKey); - foreach ($documentKeys as $documentKey) { - $this->cache->purge($documentKey); - } - - $this->cache->purge($collectionKey); - } catch (Exception $e) { - Console::warning('Failed to purge collection from cache: ' . $e->getMessage()); - } + try { + $documentKeys = $this->cache->list($collectionKey); + } catch (Exception $e) { + Console::warning('Failed to list collection cache keys: ' . $e->getMessage()); + $documentKeys = []; + } + + foreach ($documentKeys as $documentKey) { + try { + $this->cache->purge($documentKey); + } catch (Exception $e) { + Console::warning('Failed to purge cached document key: ' . $e->getMessage()); + } + } + + try { + $this->cache->purge($collectionKey); + } catch (Exception $e) { + Console::warning('Failed to purge collection cache key: ' . $e->getMessage()); + }- try { - $this->cache->purge($collectionKey, $documentKey); - $this->cache->purge($documentKey); - } catch (Exception $e) { - Console::warning('Failed to purge document from cache: ' . $e->getMessage()); - } + try { + $this->cache->purge($collectionKey, $documentKey); + } catch (Exception $e) { + Console::warning('Failed to purge document key from collection index: ' . $e->getMessage()); + } + + try { + $this->cache->purge($documentKey); + } catch (Exception $e) { + Console::warning('Failed to purge document cache key: ' . $e->getMessage()); + }Also applies to: 8248-8253
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/Database/Database.php` around lines 8217 - 8226, The current try/catch around the whole purge sequence causes a single exception from $this->cache->list or $this->cache->purge to abort all further invalidation; update the logic so each operation handles its own exceptions: wrap the call to $this->cache->list(...) in a try/catch and handle/log failures, then iterate over the returned $documentKeys and call $this->cache->purge($documentKey) inside its own try/catch so one failing key doesn't stop others, and finally call $this->cache->purge($collectionKey) in a separate try/catch with Console::warning on error; apply the same per-operation try/catch refactor to the analogous block that uses the same $this->cache->list / purge calls around lines 8248-8253.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/Database/Database.php`:
- Around line 8217-8226: The current try/catch around the whole purge sequence
causes a single exception from $this->cache->list or $this->cache->purge to
abort all further invalidation; update the logic so each operation handles its
own exceptions: wrap the call to $this->cache->list(...) in a try/catch and
handle/log failures, then iterate over the returned $documentKeys and call
$this->cache->purge($documentKey) inside its own try/catch so one failing key
doesn't stop others, and finally call $this->cache->purge($collectionKey) in a
separate try/catch with Console::warning on error; apply the same per-operation
try/catch refactor to the analogous block that uses the same $this->cache->list
/ purge calls around lines 8248-8253.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: e9fcb43b-68db-4629-bb80-83b25ebe6791
📒 Files selected for processing (1)
src/Database/Database.php
Summary
purgeCachedCollection()andpurgeCachedDocumentInternal()in try-catch blocksContext
During a production incident, cache failures on purge operations propagated as exceptions, causing HTTP request failures. This change ensures cache infrastructure issues don't cascade to application failures.
Test plan
🤖 Generated with Claude Code
Summary by CodeRabbit
Release Notes