Permalink
Browse files

better error handling

  • Loading branch information...
csquared committed May 17, 2011
1 parent e9eb8a1 commit 9f622b1528ad7a116271a05038cae2971aa5566a
Showing with 15 additions and 2 deletions.
  1. +1 −2 lib/chargify/client.rb
  2. +14 −0 test/chargify_test.rb
View
@@ -5,7 +5,7 @@ class UnexpectedResponseError < RuntimeError
class Parser < HTTParty::Parser
def parse
begin
- Crack::JSON.parse(body)
+ Crack::JSON.parse(body) || {}
rescue => e
raise UnexpectedResponseError, "Crack could not parse JSON. It said: #{e.message}. Chargify's raw response: #{body}"
end
@@ -230,7 +230,6 @@ def get(path, options={})
def jsonify_body!(options)
options[:body] = options[:body].to_json if options[:body]
-
end
end
end
View
@@ -338,6 +338,12 @@ class ChargifyTest < Test::Unit::TestCase
component.name.should == "Extra Rubies"
component.allocated_quantity.should == 42
end
+
+ should "return nil if the component is not found" do
+ stub_get "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components/16.json", "", 404
+ component = @client.subscription_component 123, 16
+ component.should == nil
+ end
should "update the allocated_quantity for a component" do
stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components/16.json", "component.json"
@@ -369,6 +375,14 @@ class ChargifyTest < Test::Unit::TestCase
response = @client.update_subscription_component_enabled 123, 16, true
response.success?.should == true
end
+
+ context "when there is an error" do
+ should "return :success? => false" do
+ stub_put "https://OU812:x@pengwynn.chargify.com/subscriptions/123/components/16.json", "", 404
+ response = @client.update_subscription_component_enabled 123, 16, true
+ response.success?.should == false
+ end
+ end
end
context "aliased methods" do

0 comments on commit 9f622b1

Please sign in to comment.