Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8259085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:46:20+00:00 2026-06-08T02:46:20+00:00

I have a simple script which checks for bad url’s: def self.check_prod_links require ‘net/http’

  • 0

I have a simple script which checks for bad url’s:

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
      begin
        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
        begin
          response = Net::HTTP.get_response("http://" + uri)  
        rescue => e
          p "Problem getting url: #{url} Error Message: #{e.message}"
        end
      end
    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

How can I allow incorrectly formatted urls eg: hkbfksrhf.google.com to not crash the script with the following error:

getaddrinfo: nodename nor servname provided, or not known

I just want the task to run till the end, and print any/all errors that are not a 200 and 301 http response.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:46:24+00:00Added an answer on June 8, 2026 at 2:46 am

    Is open-uri an option? It throws an exception when 404s or 500s (or other HTTP exceptions) are encountered, in addition to SocketErrors, which allows you to clean up your code a bit

    def self.check_prod_links                                            
      require 'open-uri'                                                 
      results = []                                                       
    
      Product.where(:published => 1).each do |product|                   
        url = product.url                                               
        id = product.id                                                  
        failed = true                                                    
    
        begin                                                            
          open URI(url)                                                  
          failed = false                                                 
        rescue OpenURI::HTTPError => e                                   
          error_message = e.message                                      
          response_message = "Response Code = #{e.io.status[0]}"         
        rescue SocketError => e                                          
          error_message = e.message                                      
          response_message = "Host unreachable"                          
        rescue => e                                                      
          error_message = e.message                                      
          response_message = "Unknown error"                             
        end                                                              
    
        if failed                                                        
          Rails.logger.error "Problem getting url: #{url} Error Message: #{error_message}"
          Rails.logger.error "Checking URL = #{url}. ID = #{id}. #{response_message}".    
    
          product.update_attribute(:published, 0).                       
          results << product                                             
        end                                                              
      end                                                                
    
      results                                                          
    end                                                                  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Perl script which runs as a Linux daemon using an
Here I have a simple php script which displays some values from a database
I have a simple script, which tries to kill an already running process. I
I have a very simple python script that should scan a text file, which
I have a simple asp page with a script including a function myFunction which
I have simple script with EventMachine, Fibers and faye require faye require em-synchrony require
I have a script which checks the variable $hello to see if it contains
I have a simple script which determines the user's IP address: function GetIp(){ if
I have a simple PHP script which generates a single email to a user
I have a simple image upload script that uses SimpleImage.php (http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/) to resize and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.