Skip to content

Commit

Permalink
Rescue RangeError and return RecordNotFound instead
Browse files Browse the repository at this point in the history
  • Loading branch information
aud committed Nov 28, 2016
1 parent e5e0edf commit c334c00
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/identity_cache/configuration_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def cache_index(*fields)
def fetch_by_#{field_list}(#{arg_list}, options={})
id = fetch_id_by_#{field_list}(#{arg_list})
id && fetch_by_id(id, options)
rescue RangeError
raise ActiveRecord::RecordNotFound
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")
end
end

private

def cache_key(unique: false)
Expand Down

0 comments on commit c334c00

Please sign in to comment.