I have a method that checks for broken links:
def self.check_prod_links
require 'net/http'
results = []
Product.find_each(:conditions =>{:published => 1}) do |product|
url = product.url
id = product.id
uri = URI(url)
begin
response = Net::HTTP.get_response(uri)
rescue
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
rescue
response = Net::HTTP.get_response("http://" + uri)
rescue => e
p "Problem getting url: #{url} Error Message: #{e.message}"
end
p "Checking URL = #{url}. ID = #{id}. Response Code = #{response.code}"
unless response.code.to_i == 200
product.update_attribute(:published, 0)
results << product
end
end
return results
end
My understanding was that rescue => e should log all exceptions not caught by the previous rescue statements, and the method should continue running, however for some reason when certain URL’s are checked, the script quits with the following exception:
SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A:
unknown protocol
How can I set this up so that if an exception is caught, it will be printed, and the task will continue running?
Also, how can I call the results array to render in the mailer view, is there a better way to do this so that I can add all products that were unpublished to an already existing mailer?
Thanks!
I’m pretty sure your first rescue will be the only one that works. Specifying
=> ejust tells Ruby to store the exception in a variable callede. I’m thinking the code in your first rescue block is raising the error you are seeing and there is nothing else rescuing it. To be honest, this is kind of a mess of code and you might be better off refactoring it into smaller methods.