Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[PATCH] ActiveResource#save! exception is invalid (causes an NoMethodError exception)

#3276

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.)

Reported by Jordan Brough · September 27th, 2009 @ 05:00 AM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Jordan Brough
    Jordan Brough
    • Tag changed from activeresource, patch to activeresource, patch

    Forgot the patch.

    September 27th, 2009 @ 05:04 AM

  2. Jordan Brough
    Jordan Brough
    • Tag changed from activeresource, patch to activeresource, bugmash, patch

    September 27th, 2009 @ 05:26 AM

  3. Jordan Brough
    Jordan Brough

    (hoping it's ok to tag this as bugmash myself)

    September 27th, 2009 @ 05:31 AM

  4. Blue Box Stephen
    Blue Box Stephen

    +1 verified

    Patch applies, tests run successfully.

    September 27th, 2009 @ 07:21 PM

  5. Gaius Centus Novus
    Gaius Centus Novus

    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.

    September 27th, 2009 @ 09:49 PM

  6. Jordan Brough
    Jordan Brough

    @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 whole ActiveResource::Base#save! concept to 2-3-stable if there's interest in that.

    September 28th, 2009 @ 02:50 PM

  7. Rizwan Reza
    Rizwan Reza
    • Tag changed from activeresource, bugmash, patch to activeresource, patch

    February 12th, 2010 @ 12:46 PM

  8. Santiago Pastorino
    Santiago Pastorino
    • 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.

    February 2nd, 2011 @ 04:55 PM

  9. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:55 PM