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

default causes try/1 to raise an ConstraintError exception rather than an Failure #469

Open
krainboltgreene opened this issue Feb 18, 2024 · 0 comments · May be fixed by #475
Open

default causes try/1 to raise an ConstraintError exception rather than an Failure #469

krainboltgreene opened this issue Feb 18, 2024 · 0 comments · May be fixed by #475

Comments

@krainboltgreene
Copy link

Describe the bug

default causes try/1 to raise an ConstraintError exception rather than an Failure. This seems completely different from every other result of try and the documentation doesn't detail this fact.

To Reproduce

Types::String.default{"x"}.try(1)
# Dry::Types::ConstraintError: 1 violates constraints (type?(String, 1) failed)
from /Users/krainboltgreene/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/dry-types-1.7.1/lib/dry/types/constrained.rb
Types::String.default("x".freeze).try(1)
# => Dry::Types::ConstraintError: 1 violates constraints (type?(String, 1) failed)
from /Users/krainboltgreene/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/dry-types-1.7.1/lib/dry/types/constrained.rb

Expected behavior

Types::String.default("x".freeze).try("")
# => #<Dry::Types::Result::Success input="">
Types::String.constrained(format: /a/i).try(1)
# => #<Dry::Types::Result::Failure input=1 error=#<Dry::Types::ConstraintError: 1 violates constraints (type?(String, 1) AND format?(/a/i, 1) failed)>>
Types::String.try(1)
# => #<Dry::Types::Result::Failure input=1 error=#<Dry::Types::ConstraintError: 1 violates constraints (type?(String, 1) failed)>>

My environment

  • Affects my production application: YES
  • Ruby version: 3.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant