Wrap corrupt cache reads in CacheCorruptError#62
Open
navidemad wants to merge 1 commit into
Open
Conversation
FileCache#read leaks JSON::ParserError and KeyError to callers when the cache file is malformed (truncated, hand-edited, partial write). The original message gives no hint that the file is FixtureKit's cache or that the fix is to delete and regenerate. Wrap both error classes in a new FixtureKit::CacheCorruptError that includes the cache path and the original error class/message, plus a short note pointing at the remediation. This is a strict superset of the previous behavior — a rescue on the new error class still catches malformed cache files, and StandardError catches everything as before. No retry, no auto-regeneration. Disk-vs-memory consistency bugs should be loud, not silently papered over.
ngan
reviewed
May 29, 2026
Collaborator
ngan
left a comment
There was a problem hiding this comment.
Thanks for the PR, left a comment.
Comment on lines
+36
to
+39
| rescue JSON::ParserError, KeyError => e | ||
| raise FixtureKit::CacheCorruptError, | ||
| "FixtureKit cache file at #{path} is corrupt or malformed (#{e.class}: #{e.message}). " \ | ||
| "Delete it and re-run to regenerate." |
Collaborator
There was a problem hiding this comment.
Can you localize these rescues so it's not rescuing errors across many statements doing very different things? Maybe move the JSON.parse(...) into its own method and do the rescue there. To DRY up the error message, you can bake it into the error class and you'd just pass in the error and the path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
FileCache#readleaksJSON::ParserErrorandKeyErrordirectly to callers when the cache file on disk is malformed (truncated, hand-edited, partially written). The resulting error gives no hint that the file is FixtureKit's cache, what path it lives at, or how to recover.Common scenarios where this surfaces:
Fix
Wrap
JSON::ParserErrorandKeyErrorraised insideFileCache#readin a newFixtureKit::CacheCorruptErrorwhose message includes the cache path, the original error class and message, and a short remediation hint ("Delete it and re-run to regenerate.").This is a strict superset of the previous behavior. A rescue on the new error class still catches malformed cache files, and
StandardErrorcatches everything as before.Design notes
Errno::ENOENTis intentionally not wrapped. Missing-file semantics differ from corrupt-file semantics, andCache#loadalready checksexists?first and raisesCacheMissingErrorfor the missing case.CacheMissingErrorinlib/fixture_kit.rb.Related
Follow-up from review feedback on #61.
Test plan
bundle exec rspec spec/unit/file_cache_spec.rb— 11/11 pass (including 2 new specs for invalid JSON and missing keys)bundle exec rspec(full suite) — 176/176 pass