Merge pull request #291 from stripe/handle-non-hash-error-responses

Handle error responses that are not hashes more gracefully
This commit is contained in:
Russell Davis 2015-09-14 21:34:37 -07:00
commit 7a0ead3fac
2 changed files with 24 additions and 1 deletions

View File

@ -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)

View File

@ -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