user2611714 user2611714 - 6 months ago 20
Ruby Question

Rails 3.2.12: Uninitialized constant Net::ReadTimeout

So, I have the following code (connection.rb):

require 'net/http'
require 'net/https'
require 'openssl'

class Connection
def request
body = '<?xml version="1.0" encoding="UTF-8"?>...'
path = '/path'
http = Net::HTTP.new('192.168.12.34', '1234')
http.read_timeout = 60
p "=1"
http.request_post(path, body, {'Content-Type' => 'application/xml'})
p "=2"
...
rescue Net::ReadTimeout => ex
...
end
end


In the string http.request_post(...) I get the output:

"=1"
NameError: uninitialized constant Net::ReadTimeout
/home/user/invp/lib/connection.rb:76:in `rescue in request'
/home/user/invp/lib/connection.rb:26:in `request'
/home/user/invp/lib/tasks/task.rake:225:in `block (2 levels) in <top (required)>'


My environment:

user@vm-dev:~/invp$ ruby --version
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]
user@vm-dev:~/invp$ rails --version
Rails 3.2.12


What's wrong?

Answer

For ruby 1.9.3 it raises TimeoutError Exception.
For ruby 2.3.1 it raises Net::ReadTimeout Exception.

So you should use rescue TimeoutError or update your ruby version.

refs (search for read_timeout at the end of the page):

http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html

http://ruby-doc.org/stdlib-2.3.1/libdoc/net/http/rdoc/Net/HTTP.html

One more information: Net::ReadTimeout is a subclass of TimeoutError and it seems to be added on ruby-2.0.0:

http://docs.ruby-lang.org/en/2.0.0/Net/ReadTimeout.html

Comments