Reputation: 67
I'm trying to validate an array of urls using mechanize. I am getting a 404 for one of the urls that is ending my loop instead of going through the rescue. I want the loop to continue even if it hits a 404. Am I doing something wrong with the begin/rescue syntax? I'm just displaying them in terminal for the time being.
a.get(url) do |page|
begin
puts url
puts page.title
rescue Mechanize::ResponseCodeError, Net::HTTPNotFound
puts "404!- " + "#{url}"
next
end
end
Upvotes: 1
Views: 777
Reputation: 10393
You need your begin/rescue/end around a.get, i.e:
begin
a.get(url) do |page|
puts url
puts page.title
end
rescue Mechanize::ResponseCodeError, Net::HTTPNotFound
puts "404!- " + "#{url}"
next
end
Upvotes: 4