
Show object.errors when raise_on_save_failure is set
Reported by Martin DeMello | June 15th, 2010 @ 01:03 PM
When I set raise_on_save_failure, the stack trace doesn't contain the contents of errors, even when available. Example:
irb(main):017:0> b.save
DataMapper::SaveFailureError: Bulletin#save returned false,
Bulletin was not saved
from /usr/lib/ruby/gems/1.9.1/gems/dm-core-1.0.0/lib/dm-core/resource.rb:1208:in `assert_save_successful'
from /usr/lib/ruby/gems/1.9.1/gems/dm-core-1.0.0/lib/dm-core/resource.rb:418:in `save'
from /usr/lib/ruby/gems/1.9.1/gems/dm-validations-1.0.0/lib/dm-validations.rb:96:in `block in save'
from /usr/lib/ruby/gems/1.9.1/gems/dm-validations-1.0.0/lib/dm-validations/support/context.rb:30:in `validation_context'
from /usr/lib/ruby/gems/1.9.1/gems/dm-validations-1.0.0/lib/dm-validations.rb:96:in `save'
from (irb):17
from /usr/bin/irb:12:in `<main>'
irb(main):018:0> b.errors
=> #<DataMapper::Validations::ValidationErrors:0x85f2018
@resource=#, @errors={:modify_date=>["Modify date must be of
type DateTime"]}>
Comments and changes to this ticket
-
Dan Kubb (dkubb) June 16th, 2010 @ 01:00 PM
- State changed from new to not-applicable
The Resource#save command can return false for reasons other than validations being invalid. If your before/after does a throw(:halt) that will stop the resource from saving. There is also dm-constraints which can do things to prevent the object from saving.
Also, the exception is defined in dm-core, and we can't tightly couple it to one of the plugins.
It is possible to rescue the exception and get at the error messages though:
begin resource.save rescue DataMapper::SaveFailureError => e puts e.resource.errors.inspect end
-
Lawrence November 23rd, 2010 @ 02:23 PM
- Milestone order changed from 0 to 0
Would it work to override #save on a per-model basis, or as a mixin, to get this more verbose error message? Or is overriding #save going to confuse DataMapper?
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »