Skip to content

Commit

Permalink
Merge pull request #191 from cllns/add-class-name-to-missing-attribut…
Browse files Browse the repository at this point in the history
…e-error

[changelog]

version: unreleased
changed: "Missing attribute error now includes the name of the class (issue #170 via #191) (@phillipoertel + @cllns)"
  • Loading branch information
solnic authored Jul 19, 2024
2 parents 2ebe5b1 + 4925db1 commit 0981ab3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/dry/struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/dry/struct/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/integration/struct_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0981ab3

Please sign in to comment.