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

Determine attribute from a CoercionError #254

Closed
karlfreeman opened this issue Mar 14, 2014 · 2 comments · Fixed by #263
Closed

Determine attribute from a CoercionError #254

karlfreeman opened this issue Mar 14, 2014 · 2 comments · Fixed by #263

Comments

@karlfreeman
Copy link

Is there a way to determine the attribute from a CoercionError?

Looking at the example below, the error messages aren't helpful enough IMO for both someone using Virtus but also as someone who could be blindly passed these error messages along.

I've had a look and it appears that this isn't supported. Just checking there isn't a reason before possibly creating a pull?

class Sandwich
  include Virtus.model(strict: true)
  attribute :bread, Symbol
  attribute :filling, Symbol
end

Sandwich.new
# Virtus::CoercionError: Failed to coerce nil into Symbol

Sandwich.new(filling: :parma_ham)
# Virtus::CoercionError: Failed to coerce nil into Symbol

Sandwich.new(bread: :sourdough)
# Virtus::CoercionError: Failed to coerce nil into Symbol

Sandwich.new(filling: :parma_ham, bread: :sourdough)
#<Sandwich:0x007ff05309ec00 @filling=:parma_ham, @bread=:sourdough>

What would be ideal would be something like this:

Sandwich.new(bread: :sourdough)
# Virtus::CoercionError: Attribute 'filling' failed to be coerced from nil into Symbol

Can you tell I'm hungry? 🍴

@ntl
Copy link
Contributor

ntl commented Apr 12, 2014

👍 to this. I prefer my domain objects to enforce strict coersion, because I usually move invariant checking -- #valid? in activerecord parlance -- to command objects (or just command methods in simple cases). Life with strict: true is pretty difficult right now, because the coersion error message doesn't tell you what attribute on what object was problematic.

I'm considering opening a PR for this, but before I started work on that I wanted to chime in on this issue.

@solnic
Copy link
Owner

solnic commented Apr 12, 2014

Yeah this should definitely be improved. It should also be a very simple thing to implement, just pass whole attribute to coercion error instead of the primitive.

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 a pull request may close this issue.

3 participants