This project is archived and is in readonly mode.

#3276 ✓stale
Jordan Brough

[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

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>

People watching this ticket

Attachments

Pages