diff --git a/lib/dry/struct.rb b/lib/dry/struct.rb index 837468c..dee4855 100644 --- a/lib/dry/struct.rb +++ b/lib/dry/struct.rb @@ -145,7 +145,9 @@ def initialize(attributes) # rom_n_roda[:title] #=> 'Web Development with ROM and Roda' # rom_n_roda[:subtitle] #=> nil def [](name) - @attributes.fetch(name) { raise MissingAttributeError, name } + @attributes.fetch(name) do + raise MissingAttributeError.new(attribute: name, klass: self.class) + end end # Converts the {Dry::Struct} to a hash with keys representing diff --git a/lib/dry/struct/errors.rb b/lib/dry/struct/errors.rb index 70a4375..02edbc2 100644 --- a/lib/dry/struct/errors.rb +++ b/lib/dry/struct/errors.rb @@ -16,8 +16,8 @@ def initialize(key) # Raised when a struct doesn't have an attribute class MissingAttributeError < ::KeyError - def initialize(key) - super("Missing attribute: #{key.inspect}") + def initialize(attribute:, klass:) + super("Missing attribute: #{attribute.inspect} on #{klass}") end end diff --git a/spec/integration/struct_spec.rb b/spec/integration/struct_spec.rb index bb5b5b1..67ef306 100644 --- a/spec/integration/struct_spec.rb +++ b/spec/integration/struct_spec.rb @@ -353,7 +353,7 @@ class Task < Dry::Struct expect { value[:name] } .to raise_error(Dry::Struct::MissingAttributeError) - .with_message("Missing attribute: :name") + .with_message("Missing attribute: :name on Test::Task") end describe "protected methods" do