1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

add rescue_json to examples

This commit is contained in:
Ruslan Korolev 2014-12-12 15:34:03 +03:00
parent 5dd1807e7b
commit c8bd4b69d3
2 changed files with 20 additions and 0 deletions

View file

@ -62,3 +62,6 @@
* Httparty included into poro class
* Uses `get` requests
* Two ways to pass params to get, inline on the url or in query hash
* [Rescue Json Error](rescue_json.rb)
* Rescue errors due to parsing response

17
examples/rescue_json.rb Normal file
View file

@ -0,0 +1,17 @@
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'httparty')
# Take note of the "; 1" at the end of the following line. It's required only if
# running this in IRB, because IRB will try to inspect the variable named
# "request", triggering the exception.
request = HTTParty.get 'https://rubygems.org/api/v1/versions/doesnotexist.json' ; 1
# Check an exception due to parsing the response
# because HTTParty evaluate the response lazily
begin
request.inspect
# This would also suffice by forcing the request to be parsed:
# request.parsed_response
rescue StandardError => e
puts "Rescued #{e.inspect}"
end