ruby rest_client exception handling
The callback block is executed only when receiving some response from the server. In this case, the name resolving is failed so RestClient.get
just throws an exception without entering the block. Thus just wrap your code within a begin...end
construct.
begin
RestClient.get('http://thisurldoesnotexist/resource') { |response, request, result, &block|
case response.code
when 200
p "It worked !"
response
else
response.return!(request, result, &block)
end
}
rescue SocketError => e
# Handle your error here
end
Ruby on rails rest client, handling error response
RestClient::Request
will raise an exception when it receives error response (response code other than 2xx/3xx):
- for result codes between 200 and 207, a RestClient::Response will be returned
- for result codes 301, 302 or 307, the redirection will be followed if the request is a GET or a HEAD
- for result code 303, the redirection will be followed and the request transformed into a GET
- for other cases, a RestClient::Exception holding the Response will be raised; a specific exception class will be thrown for known error codes
- call .response on the exception to get the server's response
Documentation
You should handle that exception:
def get url
result = RestClient::Request.execute(method: :get, url: url)
JSON.parse(result)
rescue RestClient::Exception
"error" # no need to call 'as_json'
end
More on Ruby exception handling:
Ruby Exceptions
How could I handle rest-client 500 error response and keep scraping through my loop?
You should just rescue the exception for exmaple:
[*1..3].each{|i| RestClient.get('https://fooboton.free.beeceptor.com') rescue RestClient::InternalServerError; next}
So for your case do:
CSV.open('data2.csv', 'ab') do |csv|
csv << ['Name', 'Street', 'Info', 'E-mail', 'Site']
(1..30).each do |id|
begin
response = RestClient.get(link+id.to_s)
rescue RestClient::InternalServerError
next # skip this iteration in your loop
end
... # rest of your code
Need to rescue 401 status code using RestClient
I can't tell you and why I'm sure the rest-client repo can tell you :) ... but using RestClient::Request.new
then executing the api call with a block works for me.
I think it probably has to do with the fact that the RestClient has built in exceptions.
request = RestClient::Request.new(
method: :get,
url: 'https://my-rest-service.com/resource.json')
response = request.execute {|response| response}
case response.code
when 200
puts "Good"
when 401
puts "Bad"
raise Exception
end
Restclient throwing unusual exception
So I found an answer to this, it had to do with Holger Just's answer with a minor tweak, I'm pretty sure my case is pretty unique because my company likes to hide behind a VPN script. So here's how I did it:
- First I needed to install the pre-release gem of
ffi
using the platform flag:gem install ffi --pre --platform=ruby
- Next I had to update the gem:
gem update --all
(I think that's the correct syntax for the flag)
That got ffi
working.
Related Topics
Why and When to Use the Shell Instead of Ruby
Ruby Method Like 'Self' That Refers to Instance
How to Monitor Uptime of 20 Websites (Ping or Http) in Node.Js/Ror
Ruby on Rails 3 - Public Live Chat
Asserting That a Particular Exception Is Thrown in Cucumber
Where Are the Gems When Ruby Compiled Manually in MAC Os X 10.6.8
Converting an Empty String into Nil in Ruby
How to Export a Ruby Array from My Heroku Console into CSV
Can Not Install JSON Gem with Ruby 2.2.3 on Ubuntu
Does Scala Scale Better Than Other Jvm Languages
How to Handle Exceptions with Ruby Rest-Client
Ruby Singleton Methods for Class and Objects
How to Dynamically Call Routes Helper in Rails
Rails Activeadmin: Showing Table of a Related Resource in the Same View
Getting a Dns Txt Record in Ruby
Code to Generate Gaussian (Normally Distributed) Random Numbers in Ruby