This project is archived and is in readonly mode.
[PATCH] ActiveResource#save! exception is invalid (causes an NoMethodError exception)
Reported by Jordan Brough | September 27th, 2009 @ 05:00 AM
The exception raised here:
def save!
save || raise(ResourceInvalid.new(self))
end
(on master in activeresource/lib/active_resource/base.rb - see http://github.com/rails/rails/commit/328ba3b333777bbc1269cbe0e9f590...)
is invalid because ResourceInvalid
is a
ConnectionError
and it requires an HTTP response
object (not an ActiveResource object).
Trying to inspect the raised ResourceInvalid
exception, for example, would result in an exception:
>> begin
?> raise(ActiveResource::ResourceInvalid.new(ActiveResource::Base.new))
>> rescue => e
>> puts e
>> end
NoMethodError: undefined method `code' for #<ActiveResource::Base:0x35edeb0>
from /Users/jordan/rails/web2/vendor/rails/activeresource/lib/active_resource/base.rb:1073:in `method_missing'
from /Users/jordan/rails/web2/vendor/plugins/animoto_extensions/lib/rails/active_resource/base.rb:6:in `to_s'
from (irb):4:in `puts'
from (irb):4
So we really need to supply the response object, but we'll only have a response object to supply if the validation failure was on the remote side. If client-side validations fail then no http request is made.
The problem is that ResourceInvalid is set up to represent the occurrence of a '422 Unprocessable Entity' response, not a general validation failure.
My solution is to separate out these concerns -- 422's should be
wrapped up as UnprocessableEntity
exceptions and
client/remote validation failures should be wrapped up as
ResourceInvalid
errors.
Attached is a patch that does this.
(Note: Since ActiveResource is different from ActiveRecord in
that we actually have to try to save the object before we can tell
if remote validations failed, I'm thinking that save!
should be defined first in the mixed-in
ActiveResource::Validations
module. This is reflected
in the patch.)
Comments and changes to this ticket
-
Jordan Brough September 27th, 2009 @ 05:04 AM
- Tag changed from activeresource, patch to activeresource, patch
Forgot the patch.
-
Jordan Brough September 27th, 2009 @ 05:26 AM
- Tag changed from activeresource, patch to activeresource, bugmash, patch
-
Gaius Centus Novus September 27th, 2009 @ 09:49 PM
Which version? I cannot apply this patch to 2.3 Stable.
-1 without a version. +1 if you change it to apply to 2.3 stable.
-
Jordan Brough September 28th, 2009 @ 02:50 PM
@gaius:
save!
for ActiveResource exists only on master so the patch is for master. As a separate issue I'd be willing to backport the wholeActiveResource::Base#save!
concept to 2-3-stable if there's interest in that. -
Rizwan Reza February 12th, 2010 @ 12:46 PM
- Tag changed from activeresource, bugmash, patch to activeresource, patch
-
Santiago Pastorino February 2nd, 2011 @ 04:55 PM
- State changed from new to open
- Importance changed from to
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
Santiago Pastorino February 2nd, 2011 @ 04:55 PM
- State changed from open to stale
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>