From a43da88792717f087fdf35d3c3a09e63ee5605c7 Mon Sep 17 00:00:00 2001 From: Phillip Oertel Date: Thu, 8 Jul 2021 14:28:10 +0200 Subject: [PATCH 1/3] Add class name to MissingAttributeError exception message --- lib/dry/struct.rb | 2 +- lib/dry/struct/errors.rb | 4 ++-- spec/integration/struct_spec.rb | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/dry/struct.rb b/lib/dry/struct.rb index 837468c..4b34de1 100644 --- a/lib/dry/struct.rb +++ b/lib/dry/struct.rb @@ -145,7 +145,7 @@ 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) { raise MissingAttributeError, {name: name, klass: self.class} } 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..696cff9 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(data) + super("Missing attribute: #{data[:name].inspect} on #{data[: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 From e9ce05a34e3c6350253821686ee4e5148a9f54cf Mon Sep 17 00:00:00 2001 From: Sean Collins Date: Mon, 15 Jul 2024 13:02:05 -0600 Subject: [PATCH 2/3] Use kwargs --- lib/dry/struct.rb | 2 +- lib/dry/struct/errors.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/dry/struct.rb b/lib/dry/struct.rb index 4b34de1..8bad03d 100644 --- a/lib/dry/struct.rb +++ b/lib/dry/struct.rb @@ -145,7 +145,7 @@ 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: name, klass: self.class} } + @attributes.fetch(name) { raise MissingAttributeError.new(attribute: name, klass: self.class) } 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 696cff9..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(data) - super("Missing attribute: #{data[:name].inspect} on #{data[:klass]}") + def initialize(attribute:, klass:) + super("Missing attribute: #{attribute.inspect} on #{klass}") end end From 4925db16a1c136869752f19269a0b69bbe93c5f7 Mon Sep 17 00:00:00 2001 From: Sean Collins Date: Mon, 15 Jul 2024 13:06:35 -0600 Subject: [PATCH 3/3] Fix line length --- lib/dry/struct.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/dry/struct.rb b/lib/dry/struct.rb index 8bad03d..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.new(attribute: name, klass: self.class) } + @attributes.fetch(name) do + raise MissingAttributeError.new(attribute: name, klass: self.class) + end end # Converts the {Dry::Struct} to a hash with keys representing