Skip to content

Commit

Permalink
Add support for errors in JSON format.
Browse files Browse the repository at this point in the history
[#1956 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
jakimowicz authored and jeremy committed Aug 10, 2009
1 parent 793a9f1 commit 7975885
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 26 deletions.
2 changes: 2 additions & 0 deletions activeresource/CHANGELOG
@@ -1,5 +1,7 @@
*Edge*

* Add support for errors in JSON format. #1956 [Fabien Jakimowicz]

* Recognizes 410 as Resource Gone. #2316 [Jordan Brough, Jatinder Singh]

* More thorough SSL support. #2370 [Roy Nicholson]
Expand Down
6 changes: 5 additions & 1 deletion activeresource/lib/active_resource/base.rb
Expand Up @@ -185,7 +185,7 @@ module ActiveResource
#
# Active Resource supports validations on resources and will return errors if any of these validations fail
# (e.g., "First name can not be blank" and so on). These types of errors are denoted in the response by
# a response code of <tt>422</tt> and an XML representation of the validation errors. The save operation will
# a response code of <tt>422</tt> and an XML or JSON representation of the validation errors. The save operation will
# then fail (with a <tt>false</tt> return value) and the validation errors can be accessed on the resource in question.
#
# ryan = Person.find(1)
Expand All @@ -194,10 +194,14 @@ module ActiveResource
#
# # When
# # PUT http://api.people.com:3000/people/1.xml
# # or
# # PUT http://api.people.com:3000/people/1.json
# # is requested with invalid values, the response is:
# #
# # Response (422):
# # <errors type="array"><error>First cannot be empty</error></errors>
# # or
# # {"errors":["First cannot be empty"]}
# #
#
# ryan.errors.invalid?(:first) # => true
Expand Down
24 changes: 20 additions & 4 deletions activeresource/lib/active_resource/validations.rb
Expand Up @@ -7,11 +7,10 @@ class ResourceInvalid < ClientError #:nodoc:
# Active Resource validation is reported to and from this object, which is used by Base#save
# to determine whether the object in a valid state to be saved. See usage example in Validations.
class Errors < ActiveModel::Errors
# Grabs errors from the XML response.
def from_xml(xml)
# Grabs errors from an array of messages (like ActiveRecord::Validations)
def from_array(messages)
clear
humanized_attributes = @base.attributes.keys.inject({}) { |h, attr_name| h.update(attr_name.humanize => attr_name) }
messages = Array.wrap(Hash.from_xml(xml)['errors']['error']) rescue []
messages.each do |message|
attr_message = humanized_attributes.keys.detect do |attr_name|
if message[0, attr_name.size + 1] == "#{attr_name} "
Expand All @@ -22,6 +21,18 @@ def from_xml(xml)
self[:base] << message if attr_message.nil?
end
end

# Grabs errors from the json response.
def from_json(json)
array = ActiveSupport::JSON.decode(json)['errors'] rescue []
from_array array
end

# Grabs errors from the XML response.
def from_xml(xml)
array = Array.wrap(Hash.from_xml(xml)['errors']['error']) rescue []
from_array array
end
end

# Module to support validation and errors with Active Resource objects. The module overrides
Expand Down Expand Up @@ -56,7 +67,12 @@ def save_with_validation
save_without_validation
true
rescue ResourceInvalid => error
errors.from_xml(error.response.body)
case error.response['Content-Type']
when 'application/xml'
errors.from_xml(error.response.body)
when 'application/json'
errors.from_json(error.response.body)
end
false
end

Expand Down
77 changes: 56 additions & 21 deletions activeresource/test/base_errors_test.rb
Expand Up @@ -4,45 +4,80 @@
class BaseErrorsTest < Test::Unit::TestCase
def setup
ActiveResource::HttpMock.respond_to do |mock|
mock.post "/people.xml", {}, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><errors><error>Age can't be blank</error><error>Name can't be blank</error><error>Name must start with a letter</error><error>Person quota full for today.</error></errors>", 422
mock.post "/people.xml", {}, %q(<?xml version="1.0" encoding="UTF-8"?><errors><error>Age can't be blank</error><error>Name can't be blank</error><error>Name must start with a letter</error><error>Person quota full for today.</error></errors>), 422, {'Content-Type' => 'application/xml'}
mock.post "/people.json", {}, %q({"errors":["Age can't be blank","Name can't be blank","Name must start with a letter","Person quota full for today."]}), 422, {'Content-Type' => 'application/json'}
end
@person = Person.new(:name => '', :age => '')
assert_equal @person.save, false
end

def test_should_mark_as_invalid
assert !@person.valid?
[ :json, :xml ].each do |format|
invalid_user_using_format(format) do
assert !@person.valid?
end
end
end

def test_should_parse_xml_errors
assert_kind_of ActiveResource::Errors, @person.errors
assert_equal 4, @person.errors.size
[ :json, :xml ].each do |format|
invalid_user_using_format(format) do
assert_kind_of ActiveResource::Errors, @person.errors
assert_equal 4, @person.errors.size
end
end
end

def test_should_parse_errors_to_individual_attributes
assert @person.errors[:name].any?
assert_equal ["can't be blank"], @person.errors[:age]
assert_equal ["can't be blank", "must start with a letter"], @person.errors[:name]
assert_equal ["Person quota full for today."], @person.errors[:base]
[ :json, :xml ].each do |format|
invalid_user_using_format(format) do
assert @person.errors[:name].any?
assert_equal ["can't be blank"], @person.errors[:age]
assert_equal ["can't be blank", "must start with a letter"], @person.errors[:name]
assert_equal ["Person quota full for today."], @person.errors[:base]
end
end
end

def test_should_iterate_over_errors
errors = []
@person.errors.each { |attribute, message| errors << [attribute.to_s, message] }
assert errors.include?(["name", "can't be blank"])
[ :json, :xml ].each do |format|
invalid_user_using_format(format) do
errors = []
@person.errors.each { |attribute, message| errors << [attribute, message] }
assert errors.include?([:name, "can't be blank"])
end
end
end

def test_should_iterate_over_full_errors
errors = []
@person.errors.to_a.each { |message| errors << message }
assert errors.include?("Name can't be blank")
[ :json, :xml ].each do |format|
invalid_user_using_format(format) do
errors = []
@person.errors.to_a.each { |message| errors << message }
assert errors.include?("Name can't be blank")
end
end
end

def test_should_format_full_errors
full = @person.errors.full_messages
assert full.include?("Age can't be blank")
assert full.include?("Name can't be blank")
assert full.include?("Name must start with a letter")
assert full.include?("Person quota full for today.")
[ :json, :xml ].each do |format|
invalid_user_using_format(format) do
full = @person.errors.full_messages
assert full.include?("Age can't be blank")
assert full.include?("Name can't be blank")
assert full.include?("Name must start with a letter")
assert full.include?("Person quota full for today.")
end
end
end

private
def invalid_user_using_format(mime_type_reference)
previous_format = Person.format
Person.format = mime_type_reference
@person = Person.new(:name => '', :age => '')
assert_equal false, @person.save

yield
ensure
Person.format = previous_format
end
end

2 comments on commit 7975885

@peternash
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following change to validations.rb causes a problem if the remote system returns a an XML response with:

Content-Type 'application/xml; charset=utf-8'

In this case the literal match for 'application/xml' fails and no errors are returned.

  case error.response['Content-Type']
  when 'application/xml'
    errors.from_xml(error.response.body)
  when 'application/json'
    errors.from_json(error.response.body)
  end

@csmuc
Copy link
Contributor

@csmuc csmuc commented on 7975885 Jan 4, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.