Socket error handling... Net http

loop {
res =
Net::HTTP.get_response(URI.parse(‘http://bigmac.t35.com/text.txt’))
print res.body
sleep 3
}

this loop brakes when i get the (SocketError)… i get errors because of
bad connection to my wireless network… i dont know how to handle this
error and keep the script from crashing…

:getaddrinfo: no address associated with hostname. (SocketError)
:in open' :inconnect
:in timeout' :intimeout’
:in connect :indo_star
:in `start’

On 10/02/2009 04:01 AM, Bigmac T. wrote:

error and keep the script from crashing…
I suggest you read about Ruby’s exception handling mechanisms. For a
start please look here:

http://ruby-doc.org/docs/ProgrammingRuby/

Kind regards

robert

require ‘socket’

this loop brakes when i get the (SocketError)… i get errors because of
bad connection to my wireless network… i dont know how to handle this
error and keep the script from crashing…

Try this:

loop do
begin
res =
Net::HTTP.get_response(URI.parse("http://bigmac.t35.com/text.txt
"))
print res.body
rescue SocketError => se
puts “Got socket error: #{se}”
end
sleep 3
end

7stud – wrote:

require ‘socket’

And how exactly will that help? Especially since net/http.rb already
does

require ‘net/protocol’

and net/protocol.rb does

require ‘socket’

??

Read the error message. It says:

“getaddrinfo: no address associated with hostname”

That is: there was an error in resolving a name to an IP address. Not
that a Ruby constant hadn’t been defined.

i dont know how to handle this error and keep the script from crashing.

As already mentioned here (and code provided above), one strategy is to
use
begin/rescue:

begin
some_code_here_which_can_sometimes_fail
rescue # or rescue specific errors like LoadError… or in more
# general rescue Exception => e; pp e
# ^^^ is what I often do when i just quickly want to get info. i
love pp
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs