I have several endpoints that I would like to monitor using Ruby. I would like to get their status codes (e.g. 200). I currently do this in this way
uri = URI.parse("http://example-domain.com")
response = Net::HTTP.get_response(uri)
status = response.code
However if the URL is not valid the process errors. I would like something which informs me the URL is not valid (ideally by returning "404" to the status variable)
Thanks in advance
Use
rescue
to catch exceptions. Something like this:More about exceptions you could find in the Net, for example there: http://www.tutorialspoint.com/ruby/ruby_exceptions.htm