Skip to content

Commit abe57bc

Browse files
audstephaniegiang
authored andcommitted
Rescue RangeError and return RecordNotFound instead
1 parent 5806422 commit abe57bc

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

lib/identity_cache/configuration_dsl.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ def cache_index(*fields, unique: false)
4949
def fetch_by_#{field_list}(#{arg_list}, includes: nil)
5050
id = fetch_id_by_#{field_list}(#{arg_list})
5151
id && fetch_by_id(id, includes: includes)
52+
53+
rescue RangeError
54+
raise ActiveRecord::RecordNotFound
5255
end
5356
5457
# exception throwing variant

test/index_cache_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ def test_unique_cache_index_with_non_id_primary_key
153153
assert_equal 123, KeyedRecord.fetch_by_value('a').id
154154
end
155155

156+
def test_cache_index_raises_when_range_error
157+
Item.cache_index :title, :id, unique: true
158+
assert_raises(ActiveRecord::RecordNotFound) do
159+
assert_equal @record.id, Item.fetch_by_title_and_id!("title", "1111111111111111111111111111111")
160+
end
161+
end
162+
156163
private
157164

158165
def cache_key(unique: false)

0 commit comments

Comments
 (0)