diff --git a/lib/dalli/protocol/value_serializer.rb b/lib/dalli/protocol/value_serializer.rb index f1e3cd87..b0b06ee5 100644 --- a/lib/dalli/protocol/value_serializer.rb +++ b/lib/dalli/protocol/value_serializer.rb @@ -44,7 +44,20 @@ def store(value, req_options, bitflags) def retrieve(value, bitflags) serialized = (bitflags & FLAG_SERIALIZED) != 0 - serialized ? serializer.load(value) : value + if serialized + if serializer.is_a?(Marshal) + begin + serializer.load(value) + rescue => e + raise UnmarshalError, "Unable to unmarshal value: #{e.message}" + end + else + # Use Dalli's existing exception filtering for deserialization when not using Marshal to serialize. + serializer.load(value) + end + else + value + end rescue TypeError => e filter_type_error(e) rescue ArgumentError => e diff --git a/test/protocol/test_value_serializer.rb b/test/protocol/test_value_serializer.rb index ef73ccc0..8b7d002d 100644 --- a/test/protocol/test_value_serializer.rb +++ b/test/protocol/test_value_serializer.rb @@ -158,6 +158,7 @@ describe 'when the bitflags specify serialization' do it 'should deserialize the value' do + serializer.expect :is_a?, true, [Marshal] serializer.expect :load, deserialized_dummy, [raw_value] bitflags = rand(32) bitflags |= 0x1