I have a script that calls the facebook test api to automate test account creation. At seemingly random interval, from either after 50 requests to 6000 requests, I get an exception that isn’t caught. I’m at a loss for how to figure out what the error is, so I’ll start with the relevant code here.
I’m not using the URI library because facebook keys have the pipe character that breaks URI.parse for ruby 1.8.7.
require 'rubygems'
require 'net/https'
require 'json'
http = Net::HTTP.new(domain, 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
def request_wrapper(http, request)
retry_count = 5
begin
return http.request(request)
rescue Exception => e
retry_count--
if retry_count < 0
raise e
end
retry
end
for i in 0..500 do
request = Net::HTTP::Get.new('https://' + domain + path)
response = request_wrapper(http, request)
end
The code will work for some time, but always inevitably reports the following:
in
rescue in request_wrapper': undefined method-@’ for nil:NilClass
(NoMethodError)
Anyone ever seen this undefined method ‘-@’ before? Again, this happens very intermittently, but it’s been a real thorn in my side. It always point to the line in the code where I am calling the request wrapper.
Thanks for taking a look.
The problem is with the line
retry_count--. This line gets evaluated only when a failed HTTP request raises anException, which explains why it occurs intermittently.Ruby does not have a unary decrement (
--) or increment (++) operator. Matz has outlined the philosophical reasons behind this here. Also see this thread and this one for more information.Instead,
retry_count -= 1should do the job.