简体   繁体   中英

Resolv::DNS - How to handle timeouts, errors

I'm using the follow function in ruby on rails:

  def isGoogleEmailAddress?(email_domain)
    Resolv::DNS.open({:nameserver=>["8.8.8.8"]}) do |r|
      mx = r.getresources(email_domain,Resolv::DNS::Resource::IN::MX)
      if mx.any? {|server| server.exchange.to_s.downcase.include? "google"} then
        return true
      end
      return false
    end
  end

Is there a way to handle the issue where Resolv fails, timeouts, errors etc?

Thanks

Look through the documentation for the Resolv class and add exception handlers for the various errors/exceptions the class can raise.

They're easy to pick out. Look for classes ending in error and timeout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM