From abe57bcc51bc5925d6b7d45cbae43c8a675d05cb Mon Sep 17 00:00:00 2001 From: Elliot Dohm Date: Mon, 28 Nov 2016 18:53:09 -0500 Subject: [PATCH] Rescue RangeError and return RecordNotFound instead --- lib/identity_cache/configuration_dsl.rb | 3 +++ test/index_cache_test.rb | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/lib/identity_cache/configuration_dsl.rb b/lib/identity_cache/configuration_dsl.rb index 1d8f7a5f..450acab1 100644 --- a/lib/identity_cache/configuration_dsl.rb +++ b/lib/identity_cache/configuration_dsl.rb @@ -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 diff --git a/test/index_cache_test.rb b/test/index_cache_test.rb index 842494b7..abd19cc8 100644 --- a/test/index_cache_test.rb +++ b/test/index_cache_test.rb @@ -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)