Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add class name to missing attribute error #191

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading