From c334c002fc963c006eb561405d23622943e22447 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 7da09903..d8901ffa 100644 --- a/lib/identity_cache/configuration_dsl.rb +++ b/lib/identity_cache/configuration_dsl.rb @@ -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 diff --git a/test/index_cache_test.rb b/test/index_cache_test.rb index e3a3e58e..aea07868 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)