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 authored and stephaniegiang committed Feb 13, 2018
1 parent 5806422 commit abe57bc
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 @@ -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
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 abe57bc

Please sign in to comment.