Skip to content

Commit

Permalink
Revert adding field name to constraint error
Browse files Browse the repository at this point in the history
There's a bunch of problems with that improvement:

1. It changes the type of error that is thrown by dry-types. It broke rom's specs (though I would argue it wasn't declared as part of the public interface and rom shouldn't have committed to a specific error)
2. It refers to dry-types explicitly though it wasn't a hard dependency. The following example fails with `uninitialized constant Dry::Types`:
```
class Foo
  extend Dry::Initializer

  param :foo, type: proc { Integer(_1) }
end

Foo.new('abc')
```

I decided we should ship 3.1.1 without this change and think the whole thing through. Probably, we should add a dry-types extension to dry-initializer that improves errors but requires explicit activation.
  • Loading branch information
flash-gordon committed Jan 18, 2022
1 parent 69a8d00 commit 679014f
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 39 deletions.
1 change: 0 additions & 1 deletion lib/dry/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ module Initializer
require_relative "initializer/config"
require_relative "initializer/mixin"
require_relative "initializer/dispatchers"
require_relative "initializer/errors"

# Adds methods [.[]] and [.define]
extend DSL
Expand Down
18 changes: 6 additions & 12 deletions lib/dry/initializer/builders/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,12 @@ def coercion_line
return unless @type

arity = @type.is_a?(Proc) ? @type.arity : @type.method(:call).arity
type_call_params = \
arity.equal?(1) || arity.negative? ? @val : "#{@val}, self"

<<-COERCE
begin
unless #{@null} == #{@val}
#{@val} = #{@item}.type.call(#{type_call_params})
end
rescue Dry::Types::ConstraintError => e
raise Dry::Initializer::CoercionError.new(e, '#{@source}')
end
COERCE

if arity.equal?(1) || arity.negative?
"#{@val} = #{@item}.type.call(#{@val}) unless #{@null} == #{@val}"
else
"#{@val} = #{@item}.type.call(#{@val}, self) unless #{@null} == #{@val}"
end
end

def assignment_line
Expand Down
22 changes: 0 additions & 22 deletions lib/dry/initializer/errors.rb

This file was deleted.

4 changes: 2 additions & 2 deletions spec/type_argument_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class Test::Foo
subject { Test::Foo.new 1, bar: "2" }

it "raises TypeError" do
expect { subject }.to raise_error Dry::Initializer::CoercionError, /1.* for field :foo/
expect { subject }.to raise_error Dry::Types::ConstraintError, /1/
end
end

context "in case of option mismatch" do
subject { Test::Foo.new "1", bar: 2 }

it "raises TypeError" do
expect { subject }.to raise_error Dry::Initializer::CoercionError, /2.*for field :bar/
expect { subject }.to raise_error Dry::Types::ConstraintError, /2/
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/type_constraint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Test::Foo
subject { Test::Foo.new 1 }

it "raises ArgumentError" do
expect { subject }.to raise_error Dry::Initializer::CoercionError, /1.*for field :foo/
expect { subject }.to raise_error Dry::Types::ConstraintError, /1/
end
end

Expand Down Expand Up @@ -87,7 +87,7 @@ class Test::Foo
subject { Test::Foo.new "foo" }

it "raises constraint error" do
expect { subject }.to raise_error(Dry::Initializer::CoercionError, /foo/)
expect { subject }.to raise_error(Dry::Types::ConstraintError, /foo/)
end
end

Expand Down

0 comments on commit 679014f

Please sign in to comment.