Skip to content

Commit 679014f

Browse files
committed
Revert adding field name to constraint error
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.
1 parent 69a8d00 commit 679014f

File tree

5 files changed

+10
-39
lines changed

5 files changed

+10
-39
lines changed

lib/dry/initializer.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ module Initializer
1515
require_relative "initializer/config"
1616
require_relative "initializer/mixin"
1717
require_relative "initializer/dispatchers"
18-
require_relative "initializer/errors"
1918

2019
# Adds methods [.[]] and [.define]
2120
extend DSL

lib/dry/initializer/builders/attribute.rb

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,12 @@ def coercion_line
7272
return unless @type
7373

7474
arity = @type.is_a?(Proc) ? @type.arity : @type.method(:call).arity
75-
type_call_params = \
76-
arity.equal?(1) || arity.negative? ? @val : "#{@val}, self"
77-
78-
<<-COERCE
79-
begin
80-
unless #{@null} == #{@val}
81-
#{@val} = #{@item}.type.call(#{type_call_params})
82-
end
83-
rescue Dry::Types::ConstraintError => e
84-
raise Dry::Initializer::CoercionError.new(e, '#{@source}')
85-
end
86-
COERCE
75+
76+
if arity.equal?(1) || arity.negative?
77+
"#{@val} = #{@item}.type.call(#{@val}) unless #{@null} == #{@val}"
78+
else
79+
"#{@val} = #{@item}.type.call(#{@val}, self) unless #{@null} == #{@val}"
80+
end
8781
end
8882

8983
def assignment_line

lib/dry/initializer/errors.rb

Lines changed: 0 additions & 22 deletions
This file was deleted.

spec/type_argument_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ class Test::Foo
1515
subject { Test::Foo.new 1, bar: "2" }
1616

1717
it "raises TypeError" do
18-
expect { subject }.to raise_error Dry::Initializer::CoercionError, /1.* for field :foo/
18+
expect { subject }.to raise_error Dry::Types::ConstraintError, /1/
1919
end
2020
end
2121

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

2525
it "raises TypeError" do
26-
expect { subject }.to raise_error Dry::Initializer::CoercionError, /2.*for field :bar/
26+
expect { subject }.to raise_error Dry::Types::ConstraintError, /2/
2727
end
2828
end
2929

spec/type_constraint_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Test::Foo
5050
subject { Test::Foo.new 1 }
5151

5252
it "raises ArgumentError" do
53-
expect { subject }.to raise_error Dry::Initializer::CoercionError, /1.*for field :foo/
53+
expect { subject }.to raise_error Dry::Types::ConstraintError, /1/
5454
end
5555
end
5656

@@ -87,7 +87,7 @@ class Test::Foo
8787
subject { Test::Foo.new "foo" }
8888

8989
it "raises constraint error" do
90-
expect { subject }.to raise_error(Dry::Initializer::CoercionError, /foo/)
90+
expect { subject }.to raise_error(Dry::Types::ConstraintError, /foo/)
9191
end
9292
end
9393

0 commit comments

Comments
 (0)