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

Avoid throwing an exception for failed coercions with dry-types 1.x #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

shepmaster
Copy link

dry-types 0.15 and 1.x changed how failed coercions are reported. Older versions returned the original value while newer versions raise an exception.

0.14.1:

Types::Params::Integer['cow']
 # => "cow"

1.7.1:

Types::Params::Integer['cow']
 # *** Dry::Types::CoercionError Exception: invalid value for Integer(): "cow"

Fortunately, the constructor accepts an optional block:

When a block is passed, {#call} will never throw an exception on
failed coercion, instead it will call the block.

Doubly-fortunately, the block appears to be ignored on older versions of dry-types.

To preserve the current behavior, we update to use a block that returns the original value.

dry-types 0.15 and 1.x changed how failed coercions are
reported. Older versions returned the original value while newer
versions raise an exception.

0.14.1:

```ruby
Types::Params::Integer['cow']
 # => "cow"
```

1.7.1:

```ruby
Types::Params::Integer['cow']
 # *** Dry::Types::CoercionError Exception: invalid value for Integer(): "cow"
```

Fortunately, the constructor accepts an optional block:

> When a block is passed, `{#call}` will never throw an exception on
> failed coercion, instead it will call the block.

Doubly-fortunately, the block appears to be ignored on older versions
of dry-types.

To preserve the current behavior, we update to use a block that
returns the original value.
@apotonick
Copy link
Owner

In CI I see two failures:

   1) Failure:
without Type With Nilify#test_0001_raise error for new dry-types v - work as expected for older versions [/home/runner/work/disposable/disposable/test/twin/coercion_test.rb:138]:
Dry::Types::CoercionError expected but nothing was raised.

  2) Failure:
CoercionTypingTest#test_0001_anonymous [/home/runner/work/disposable/disposable/test/twin/coercion_test.rb:184]:
Dry::Types::ConstraintError expected but nothing was raised.

By always adding that "soft block" we apparently changed expected behavior, need to check out what's the best way to do this.

thanks for this PR! 💚

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants