Permalink
Browse files

include more details in error in case the HTTP request doesn't return…

… valid JSON
  • Loading branch information...
1 parent 9ffc661 commit 8da8ddd58497a04cea6cb8733de7bc738876bf24 @mreinsch mreinsch committed Apr 5, 2013
Showing with 31 additions and 9 deletions.
  1. +11 −4 lib/paypal_adaptive/request.rb
  2. +1 −0 paypal_adaptive.gemspec
  3. +4 −1 test/test_helper.rb
  4. +15 −4 test/unit/request_test.rb
@@ -81,8 +81,15 @@ def wrap_post(data, path)
PaypalAdaptive::Response.new(post(data, path), @env)
end
- def rescue_error_message(e, message = nil)
- {"responseEnvelope" => {"ack" => "Failure"}, "error" => [{"message" => (message ||= e)}]}
+ def rescue_error_message(e, message = nil, response_data = nil)
+ error = {"message" => message || e}
+ if response_data
+ error["details"] = {
+ "httpCode" => response_data.code,
+ "httpMessage" => response_data.message.to_s,
+ "httpBody" => response_data.body }
+ end
+ {"responseEnvelope" => {"ack" => "Failure"}, "error" => [error]}
end
def post(data, path)
@@ -107,13 +114,13 @@ def post(data, path)
rescue Net::HTTPBadGateway => e
rescue_error_message(e, "Error reading from remote server.")
rescue JSON::ParserError => e
- rescue_error_message(e, "Response is not in JSON format.")
+ rescue_error_message(e, "Response is not in JSON format.", response_data)
rescue Exception => e
case e
when Errno::ECONNRESET
rescue_error_message(e, "Connection Reset. Request invalid URL.")
else
- rescue_error_message(e)
+ rescue_error_message(e, response_data)
end
end
View
@@ -16,6 +16,7 @@ Gem::Specification.new do |s|
s.add_dependency("jsonschema", "~>2.0.0")
s.add_development_dependency("rake")
s.add_development_dependency("activesupport", "~> 3.2.0")
+ s.add_development_dependency("webmock")
s.rubyforge_project = "paypal_adaptive"
View
@@ -6,4 +6,7 @@
require "json"
require "jsonschema"
require 'paypal_adaptive'
-require 'active_support/core_ext/string'
+require 'active_support/core_ext/string'
+require 'webmock/minitest'
+
+WebMock.allow_net_connect!
View
@@ -12,16 +12,27 @@ def test_post_should_return_hash
assert_instance_of Hash, response
end
- def test_post_should_return_hash_when_request_is_invalid
+ def test_post_should_return_error_message_when_request_is_invalid
request = PaypalAdaptive::Request.new('test')
response = request.post({:data => true}, '/some-random-url')
assert_instance_of Hash, response
+ assert_equal "Connection Reset. Request invalid URL.", response["error"][0]["message"]
end
- def test_post_shoudl_return_error_message_when_request_is_invalid
+ def test_post_should_return_error_details_when_response_is_invalid
+ WebMock.disable_net_connect!
+ stub_request(:post, "https://svcs.sandbox.paypal.com/some-random-url").
+ to_return(:status => [500, "Internal Server Error"], :body => "Something went wrong")
request = PaypalAdaptive::Request.new('test')
response = request.post({:data => true}, '/some-random-url')
- assert_not_nil response["error"][0]["message"]
+ assert_instance_of Hash, response
+ error = response["error"].first
+ assert_instance_of Hash, error
+ assert_equal "Response is not in JSON format.", error["message"]
+ assert_equal "500", error["details"]["httpCode"]
+ assert_equal "Internal Server Error", error["details"]["httpMessage"]
+ assert_equal "Something went wrong", error["details"]["httpBody"]
+ ensure
+ WebMock.allow_net_connect!
end
-
end

0 comments on commit 8da8ddd

Please sign in to comment.