[Git][noosfero/noosfero][master] 2 commits: api: improve error handling for models
Leandro Nunes
gitlab at mg.gitlab.com
Fri May 5 15:13:46 BRT 2017
Leandro Nunes pushed to branch master at Noosfero / noosfero
Commits:
0bdcae86 by Victor Costa at 2017-05-05T14:34:17-03:00
api: improve error handling for models
- - - - -
0fbfcad2 by Leandro Nunes at 2017-05-05T18:13:30+00:00
Merge branch 'api-model-errors' into 'master'
api: improve error handling for models
See merge request !1199
- - - - -
4 changed files:
- app/api/helpers.rb
- test/api/articles_test.rb
- test/api/helpers_test.rb
- test/api/profiles_test.rb
Changes:
=====================================
app/api/helpers.rb
=====================================
--- a/app/api/helpers.rb
+++ b/app/api/helpers.rb
@@ -406,13 +406,13 @@ module Api
def render_model_errors!(active_record_errors)
message_hash = {}
- message_hash[:errors_details] = active_record_errors.details if active_record_errors.details
- message_hash[:errors_messages] = active_record_errors.messages if active_record_errors.messages
- message_hash[:full_messages] = active_record_errors.full_messages if active_record_errors.full_messages
- full_messages = ''
- full_messages = active_record_errors.full_messages.join(', ') if active_record_errors.full_messages
- log_message = "#{status}, model errors: #{full_messages}"
- logger.error log_message unless Rails.env.test?
+ if active_record_errors.details
+ message_hash[:errors] = active_record_errors.details
+ message_hash[:errors].each do |field, errors|
+ full_messages = active_record_errors.full_messages_for(field)
+ errors.each_with_index {|error, i| error[:full_message] = full_messages[i] }
+ end
+ end
error!(message_hash, Api::Status::UNPROCESSABLE_ENTITY)
end
=====================================
test/api/articles_test.rb
=====================================
--- a/test/api/articles_test.rb
+++ b/test/api/articles_test.rb
@@ -868,9 +868,7 @@ class ArticlesTest < ActiveSupport::TestCase
params[:article] = {:name => ""}
post "/api/v1/communities/#{profile.id}/articles?#{params.to_query}"
json = JSON.parse(last_response.body)
- assert_equal ({"name" => [{"error"=>"blank"}]}), json["errors_details"]
- assert_equal ({"name"=>["can't be blank"]}), json["errors_messages"]
- assert_equal (["Title can't be blank"]), json["full_messages"]
+ assert_equal ({"name" => [{"error"=>"blank", "full_message"=>"Title can't be blank"}]}), json["errors"]
end
end
=====================================
test/api/helpers_test.rb
=====================================
--- a/test/api/helpers_test.rb
+++ b/test/api/helpers_test.rb
@@ -264,6 +264,17 @@ class Api::HelpersTest < ActiveSupport::TestCase
assert 2, parse_parent_id(2)
end
+ should 'return errors with full messages' do
+ object = Person.new
+ object.valid?
+ hash = render_model_errors!(object.errors)
+ expected = [
+ {error: :blank, full_message: "Identifier can't be blank"},
+ {error: :not_available, full_message: "Identifier is not available."}
+ ]
+ assert_equal expected, hash.first[:errors][:identifier]
+ end
+
protected
def error!(info, status)
=====================================
test/api/profiles_test.rb
=====================================
--- a/test/api/profiles_test.rb
+++ b/test/api/profiles_test.rb
@@ -342,8 +342,8 @@ class ProfilesTest < ActiveSupport::TestCase
post "/api/v1/profiles/#{person.id}?#{params.to_query}"
json = JSON.parse(last_response.body)
assert_equal Api::Status::UNPROCESSABLE_ENTITY, last_response.status
- assert_equal "blank", json['errors_details']['name'].first['error']
- assert_equal "not_available", json['errors_details']['identifier'].first['error']
+ assert_equal "blank", json['errors']['name'].first['error']
+ assert_equal "not_available", json['errors']['identifier'].first['error']
end
should 'add block in a profile' do
@@ -403,9 +403,7 @@ class ProfilesTest < ActiveSupport::TestCase
params[:profile][:name] = ''
post "/api/v1/profiles/#{person.id}?#{params.to_query}"
json = JSON.parse(last_response.body)
- assert_equal ({"name" => [{"error"=>"blank"}]}), json["errors_details"]
- assert_equal ({"name"=>["can't be blank"]}), json["errors_messages"]
- assert_equal (["Name can't be blank"]), json["full_messages"]
+ assert_equal ({"name" => [{"error"=>"blank", "full_message"=>"Name can't be blank"}]}), json["errors"]
end
end
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/8ed8d821c43c5c6349e764d52e77ee55dca92d2b...0fbfcad262b8e10c02fc22753ea9a347dccea59c
---
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/8ed8d821c43c5c6349e764d52e77ee55dca92d2b...0fbfcad262b8e10c02fc22753ea9a347dccea59c
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20170505/d541a160/attachment-0001.html>
More information about the Noosfero-dev
mailing list