Dry::Struct: improve exception message on MissingAttributeError

Hi everyone,

in our current project, we use Dry::Struct extensively. Our Dry::Structs are often nested (right now up to three levels). Here’s an example:


module Core
  module Models
    class Claim < Dry::Struct
      attribute :policy, Core::Models::Policy
      # ...
    end
  end
end

module Core
  module Models
    class Policy < Dry::Struct
      # ...
    end
  end
end

When creating new instances of our structs, we often get this error:

      Dry::Struct::MissingAttributeError:
       Missing attribute: :transport

Due to the nesting, and the fact that we use Dry::Struct in many places (not just the model layer) it’s
often difficult to find out where the attribute is missing.

An improved exception message of Dry::Struct::MissingAttributeError would thus be (example):

      Dry::Struct::MissingAttributeError:
       Missing attribute: :transport on Core::Models::Claim

Here’s a PR to implement that: Add class name to MissingAttributeError exception message by phillipoertel · Pull Request #170 · dry-rb/dry-struct · GitHub.
We’d be happy if you find this addition helpful too.

Cheers
Phillip

2 Likes

Hi, any thoughts on this?

Thoughts: it looks good. :slight_smile: We’ll merge it sometime soon, thank you. There are some other things I’d like to improve about type errors btw so I hope to bundle them.

1 Like

Cool, thank you.

1 Like

Hi guys, I just ran across our dry-struct monkey patch to fix this issue, and was wondering if you’d add it. My PR is still available, and would merge nicely.
Cheers, Phillip