Skip to content

Commit

Permalink
raise an exception if the API call returns an error
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmycuadra committed Dec 25, 2010
1 parent 38826d5 commit 143fab1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/to_lang/connector.rb
Expand Up @@ -13,6 +13,7 @@ def initialize(key)


def request(q, target, *args) def request(q, target, *args)
response = HTTParty.get request_url(q, target, *args) response = HTTParty.get request_url(q, target, *args)
raise response.parsed_response["error"]["message"] if response.parsed_response["error"] && response.parsed_response["error"]["message"]
response.parsed_response["data"]["translations"][0]["translatedText"] response.parsed_response["data"]["translations"][0]["translatedText"]
end end


Expand Down
7 changes: 7 additions & 0 deletions spec/to_lang/connector_spec.rb
Expand Up @@ -47,5 +47,12 @@ def stub_bad_response(error_message)
end end
end end
end end

context "with a bad language pair" do
it "raises an exception" do
stub_bad_response "Bad language pair: en|en"
expect { @connector.request("a pie", "en", :from => "en") }.to raise_error(RuntimeError, "Bad language pair: en|en")
end
end
end end
end end

0 comments on commit 143fab1

Please sign in to comment.