dry-struct icon indicating copy to clipboard operation
dry-struct copied to clipboard

Errors in structs nested inside a Types::Hash raise as `#<NoMethodError: undefined method 'message' for #<String` instead of Dry Errors with the actual message

Open jornvandebeek opened this issue 2 years ago • 0 comments

Describe the bug

When nesting a dry struct in another dry struct through a Hash attribute, dry errors in the nested struct are not raised as dry errors, but as a nomethod error from inside the dry library.

To Reproduce

Failing spec:

  module DryTypes
    include Dry.Types()
  end

  class NestedStruct < Dry::Struct
    attribute :something, DryTypes::String
  end

  class TestStruct < Dry::Struct
    attribute :hash, DryTypes::Hash.map(DryTypes::Coercible::Symbol, NestedStruct)
  end

  it 'throws a dry error if a nested attribute is missing' do
    expect do
      TestStruct.new({hash: {first: {}}})
    end.to raise_exception(Dry::Struct::Error)
  end

Will error with :

expected Dry::Struct::Error, got #<NoMethodError: undefined method 'message' for "[NestedStruct.new] :something is missing in Hash input":String> with backtrace: # ~/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/dry-types-1.5.1/lib/dry/types/errors.rb:59:in `map'

Expected behavior

The error raised should be a Dry error with the correct message.

Dry should have assigned #error on the nested struct as an exception instead of a string to make MultipleError handle it correctly?

jornvandebeek avatar Nov 10 '21 13:11 jornvandebeek