diff --git a/ruby_event_store/lib/ruby_event_store/errors.rb b/ruby_event_store/lib/ruby_event_store/errors.rb index 2b1fb8146e..ca3923d31d 100644 --- a/ruby_event_store/lib/ruby_event_store/errors.rb +++ b/ruby_event_store/lib/ruby_event_store/errors.rb @@ -1,17 +1,19 @@ module RubyEventStore - WrongExpectedEventVersion = Class.new(StandardError) - InvalidExpectedVersion = Class.new(StandardError) - IncorrectStreamData = Class.new(StandardError) - SubscriberNotExist = Class.new(StandardError) - InvalidPageStart = Class.new(ArgumentError) - InvalidPageSize = Class.new(ArgumentError) - EventDuplicatedInStream = Class.new(StandardError) - NotSupported = Class.new(StandardError) - ReservedInternalName = Class.new(StandardError) - InvalidHandler = Class.new(StandardError) + WrongExpectedEventVersion = Class.new(StandardError) + InvalidExpectedVersion = Class.new(StandardError) + IncorrectStreamData = Class.new(StandardError) + SubscriberNotExist = Class.new(StandardError) + InvalidPageStart = Class.new(ArgumentError) + InvalidPageSize = Class.new(ArgumentError) + EventDuplicatedInStream = Class.new(StandardError) + NotSupported = Class.new(StandardError) + ReservedInternalName = Class.new(StandardError) + InvalidHandler = Class.new(StandardError) + Mappers::Protobuf::InvalidData = Class.new(StandardError) class EventNotFound < StandardError attr_reader :event_id + def initialize(event_id) super("Event not found: #{event_id}") @event_id = event_id diff --git a/ruby_event_store/lib/ruby_event_store/mappers/protobuf.rb b/ruby_event_store/lib/ruby_event_store/mappers/protobuf.rb index ad99859c0c..9734687b6d 100644 --- a/ruby_event_store/lib/ruby_event_store/mappers/protobuf.rb +++ b/ruby_event_store/lib/ruby_event_store/mappers/protobuf.rb @@ -1,9 +1,5 @@ module RubyEventStore class Proto < RubyEventStore::Event - def initialize(event_id: SecureRandom.uuid, metadata: nil, data:) - super - end - def type data.class.descriptor.name end @@ -45,8 +41,8 @@ def initialize(events_class_remapping: {}) def event_to_serialized_record(domain_event) SerializedRecord.new( event_id: domain_event.event_id, - metadata: ProtobufNestedStruct::HashMapStringValue.dump(domain_event.metadata.each_with_object({}){|(k,v),h| h[k.to_s] =v }), - data: domain_event.data.class.encode(domain_event.data), + metadata: ProtobufNestedStruct::HashMapStringValue.dump(domain_event.metadata.each_with_object({}) {|(k, v), h| h[k.to_s] = v}), + data: encode_data(domain_event.data), event_type: domain_event.type ) end @@ -74,6 +70,14 @@ def load_data(event_type, protobuf_data) Google::Protobuf::DescriptorPool.generated_pool.lookup(event_type).msgclass.decode(protobuf_data) end + def encode_data(domain_event_data) + begin + domain_event_data.class.encode(domain_event_data) + rescue NoMethodError + raise InvalidData + end + end + def require_optional_dependency require 'protobuf_nested_struct' rescue LoadError diff --git a/ruby_event_store/spec/mappers/protobuf_spec.rb b/ruby_event_store/spec/mappers/protobuf_spec.rb index 41d4576563..3d5b3a2dd6 100644 --- a/ruby_event_store/spec/mappers/protobuf_spec.rb +++ b/ruby_event_store/spec/mappers/protobuf_spec.rb @@ -253,6 +253,31 @@ def p.require(_name) expect(event.data.class).to eq(ResTesting::OrderCreated) expect(event.type).to eq("res_testing.OrderCreated") end + + specify '#event_to_serialized_record raises error when no data' do + domain_event = + RubyEventStore::Proto.new( + event_id: "f90b8848-e478-47fe-9b4a-9f2a1d53622b", + data: nil, + metadata: metadata, + ) + expect do + subject.event_to_serialized_record(domain_event) + end.to raise_error(Protobuf::InvalidData) + end + + specify '#event_to_serialized_record raises error when wrong data' do + domain_event = + RubyEventStore::Proto.new( + event_id: "f90b8848-e478-47fe-9b4a-9f2a1d53622b", + data: {}, + metadata: metadata, + ) + + expect do + subject.event_to_serialized_record(domain_event) + end.to raise_error(Protobuf::InvalidData) + end end end end