Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Handle request exceptions gracefully #46

Merged
merged 2 commits into from

2 participants

@jalvarezsamayoa

I've added a rescue block around the post call to Paypal. The idea is to be able to handle server errors (like when sandbox goes down) gracefully.

Let me know if this is ok or would you like me to refine the error handling.

I was thinking trying to duplicate the Paypal's complete response structure or at least some basic fields like responseEvelope.ack so people can check if there was a problem with the request.

@tc
Owner
tc commented

Can you incorporate the http response code in the exception?

Also, you don't need the "return" keyword in ruby. The last value will be returned.

@tc tc merged commit c14c580 into tc:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 44 additions and 2 deletions.
  1. +17 −2 lib/paypal_adaptive/request.rb
  2. +27 −0 test/unit/request_test.rb
View
19 lib/paypal_adaptive/request.rb
@@ -75,6 +75,10 @@ def wrap_post(data, path)
PaypalAdaptive::Response.new(post(data, path), @env)
end
+ def rescue_error_message(e, message = nil)
+ {"responseEvelope" => {"ack" => "Failure"}, "error" => [{"message" => (message ||= e)}]}
+ end
+
def post(data, path)
#hack fix: JSON.unparse doesn't work in Rails 2.3.5; only {}.to_json does..
api_request_data = JSON.unparse(data) rescue data.to_json
@@ -90,9 +94,20 @@ def post(data, path)
end
http.ca_path = @ssl_cert_path unless @ssl_cert_path.nil?
- response_data = http.post(path, api_request_data, @headers).body
+ begin
+ response_data = http.post(path, api_request_data, @headers)
+ return JSON.parse(response_data.body)
+ rescue Net::HTTPBadGateway => e
+ rescue_error_message(e, "Error reading from remote server.")
+ rescue Exception => e
+ case e
+ when Errno::ECONNRESET
+ rescue_error_message(e, "Connection Reset. Request invalid URL.")
+ else
+ rescue_error_message(e)
+ end
+ end
- JSON.parse(response_data)
end
end
View
27 test/unit/request_test.rb
@@ -0,0 +1,27 @@
+require 'test_helper'
+
+class RequestTest < Test::Unit::TestCase
+ def setup
+ @schema_filepath = File.join(File.dirname(__FILE__),"..", "..", "lib","pay_request_schema.json")
+ @schema = File.open(@schema_filepath, "rb"){|f| JSON.parse(f.read)}
+ end
+
+ def test_post_should_return_hash
+ request = PaypalAdaptive::Request.new('test')
+ response = request.post({:data => true}, '/AdaptivePayments/Pay')
+ assert_instance_of Hash, response
+ end
+
+ def test_post_should_return_hash_when_request_is_invalid
+ request = PaypalAdaptive::Request.new('test')
+ response = request.post({:data => true}, '/some-random-url')
+ assert_instance_of Hash, response
+ end
+
+ def test_post_shoudl_return_error_message_when_request_is_invalid
+ request = PaypalAdaptive::Request.new('test')
+ response = request.post({:data => true}, '/some-random-url')
+ assert_not_nil response["error"][0]["message"]
+ end
+
+end
Something went wrong with that request. Please try again.