Please review pull request #573: Provide a helpful error message when name resolution fails opened by (nicklewis)

Description:

Previously, this would print a message like:

getaddrinfo: nodename nor servname not provided, or not found

Which is opaque to anyone who doesn't already know what it means. This is a
very common case (it happens, for instance, when first starting a default agent
without setting up a DNS entry for 'puppet'). Now, we catch SocketError when
making an HTTP request, and print a more useful error message if this is the
particular failure case.

  • Opened: Sat Mar 10 00:32:10 UTC 2012
  • Based on: puppetlabs:master (597d134b6555a3c96034f33db0d66faa60f22bb2)
  • Requested merge: nicklewis:helpful-name-resolution-failure-message (aae3a58b7d241b33b7a12ff2aadafa2db3b4904d)

Diff follows:

diff --git a/lib/puppet/indirector/rest.rb b/lib/puppet/indirector/rest.rb
index b9b68b0..9286b71 100644
--- a/lib/puppet/indirector/rest.rb
+++ b/lib/puppet/indirector/rest.rb
@@ -101,6 +101,13 @@ def http_request(method, request, *args)
     end
 
     http_connection.send(method, *args)
+  rescue SocketError => error
+    case error.message
+    when /nodename nor servname provided, or not known/
+      raise Puppet::Error, "Could not resolve hostname '#{http_connection.address}'"
+    else
+      raise
+    end
   rescue OpenSSL::SSL::SSLError => error
     if error.message.include? "certificate verify failed"
       raise Puppet::Error, "#{error.message}.  This is often because the time is out of sync on the server or client"
diff --git a/spec/unit/indirector/rest_spec.rb b/spec/unit/indirector/rest_spec.rb
index e664ef2..a643f3c 100755
--- a/spec/unit/indirector/rest_spec.rb
+++ b/spec/unit/indirector/rest_spec.rb
@@ -93,6 +93,13 @@ module Test
   describe "when making http requests" do
     include PuppetSpec::Files
 
+    it "should provide a helpful error message when name resolution fails" do
+      connection = Net::HTTP.new('a_hopefully_unresolvable_name', 0)
+      @searcher.stubs(:network).returns connection
+
+      expect { @searcher.http_request(:get, stub('request'), '/') }.to raise_error(/Could not resolve hostname 'a_hopefully_unresolvable_name'/)
+    end
+
     it "should provide a suggestive error message when certificate verify failed" do
       connection = Net::HTTP.new('my_server', 8140)
       @searcher.stubs(:network).returns(connection)

    

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to