Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rescue RangeError and return RecordNotFound instead #299

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/identity_cache/configuration_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def cache_index(*fields, unique: false)
def fetch_by_#{field_list}(#{arg_list}, includes: nil)
id = fetch_id_by_#{field_list}(#{arg_list})
id && fetch_by_id(id, includes: includes)

rescue RangeError
raise ActiveRecord::RecordNotFound
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method shouldn't raise. The exception throwing variant is below and already does a raise ActiveRecord::RecordNotFound if this method returns nil.

If we want to workaround this issue in identity cache, then it should probably be done in fetch_by_id and dynamic_attribute_cache_miss for unique_index. That way the fix would at least be more consistently applied across identity cache.

end

# exception throwing variant
Expand Down
7 changes: 7 additions & 0 deletions test/index_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ def test_unique_cache_index_with_non_id_primary_key
assert_equal 123, KeyedRecord.fetch_by_value('a').id
end

def test_cache_index_raises_when_range_error
Item.cache_index :title, :id, unique: true
assert_raises(ActiveRecord::RecordNotFound) do
assert_equal @record.id, Item.fetch_by_title_and_id!("title", "1111111111111111111111111111111")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be another test to make sure Item.fetch_by_title_and_id("title", "1111111111111111111111111111111") doesn't raise

end
end

private

def cache_key(unique: false)
Expand Down