diff --git a/lib/stripe.rb b/lib/stripe.rb index 8f755880..a68912eb 100644 --- a/lib/stripe.rb +++ b/lib/stripe.rb @@ -253,7 +253,8 @@ module Stripe begin error_obj = JSON.parse(resp.body) error_obj = Util.symbolize_names(error_obj) - error = error_obj[:error] or raise StripeError.new # escape from parsing + error = error_obj[:error] + raise StripeError.new unless error && error.is_a?(Hash) rescue JSON::ParserError, StripeError raise general_api_error(resp.code, resp.body) diff --git a/test/stripe/api_resource_test.rb b/test/stripe/api_resource_test.rb index a68b16dd..fe63cb33 100644 --- a/test/stripe/api_resource_test.rb +++ b/test/stripe/api_resource_test.rb @@ -125,6 +125,28 @@ module Stripe 'sk_test_local') end + should "handle error response with empty body" do + response = make_response('', 500) + @mock.expects(:post).once.raises(RestClient::ExceptionWithResponse.new(response, 500)) + + e = assert_raises Stripe::APIError do + Stripe::Charge.create + end + + assert_equal 'Invalid response object from API: "" (HTTP response code was 500)', e.message + end + + should "handle error response with non-object error value" do + response = make_response('{"error": "foo"}', 500) + @mock.expects(:post).once.raises(RestClient::ExceptionWithResponse.new(response, 500)) + + e = assert_raises Stripe::APIError do + Stripe::Charge.create + end + + assert_equal 'Invalid response object from API: "{\"error\": \"foo\"}" (HTTP response code was 500)', e.message + end + should "have default open and read timeouts" do assert_equal Stripe.open_timeout, 30 assert_equal Stripe.read_timeout, 80