Please review pull request #116: (#7753) Added error checking when adding resolves opened by (adrienthebo)

Description:

Added exception handling to the fact class. When adding a resolution to
a fact, if an exception was thrown outside of the setcode block, facter
would crash. Added handling so that if an exception is thrown, facter
logs the error and discards the fact.

  • Opened: Tue Dec 13 19:09:44 UTC 2011
  • Based on: puppetlabs:1.6.x (7065eeeafd668a874ae2e421a0b7554043a71594)
  • Requested merge: adrienthebo:ticket/1.6.x/7753-catch_exceptions_when_adding_facts (cb4e29412edb8af63ddfb02a3df1937b5fa6a4a6)

Diff follows:

diff --git a/lib/facter/util/collection.rb b/lib/facter/util/collection.rb
index d165ff0..29e6714 100644
--- a/lib/facter/util/collection.rb
+++ b/lib/facter/util/collection.rb
@@ -31,9 +31,8 @@ def add(name, options = {}, &block)
       end
     end
 
-    if block
-      resolve = fact.add(&block)
-      # Set any resolve-appropriate options
+    if block_given? and resolve = fact.add(&block)
+      # If the resolve was actually added, set any resolve-appropriate options
       options.each do |opt, value|
         method = opt.to_s + "="
         if resolve.respond_to?(method)
diff --git a/lib/facter/util/fact.rb b/lib/facter/util/fact.rb
index 070087b..3f18d7f 100644
--- a/lib/facter/util/fact.rb
+++ b/lib/facter/util/fact.rb
@@ -33,17 +33,22 @@ def initialize(name, options = {})
   def add(&block)
     raise ArgumentError, "You must pass a block to Fact<instance>.add" unless block_given?
 
-    resolve = Facter::Util::Resolution.new(@name)
+    begin
+      resolve = Facter::Util::Resolution.new(@name)
 
-    resolve.instance_eval(&block)
+      resolve.instance_eval(&block)
 
-    @resolves << resolve
+      @resolves << resolve
 
-    # Immediately sort the resolutions, so that we always have
-    # a sorted list for looking up values.
-    @resolves.sort! { |a, b| b.weight <=> a.weight }
+      # Immediately sort the resolutions, so that we always have
+      # a sorted list for looking up values.
+      @resolves.sort! { |a, b| b.weight <=> a.weight }
 
-    return resolve
+      resolve
+    rescue => e
+      Facter.warn "Unable to add resolve for #{@name}: #{e}"
+      nil
+    end
   end
 
   # Flush any cached values.
diff --git a/spec/unit/util/collection_spec.rb b/spec/unit/util/collection_spec.rb
index c676727..1a6537a 100755
--- a/spec/unit/util/collection_spec.rb
+++ b/spec/unit/util/collection_spec.rb
@@ -93,6 +93,16 @@
 
         @coll.add(:myname) {}
       end
+
+      it "should discard resolutions that throw an exception when added" do
+        lambda {
+          @coll.add('yay') do
+            raise
+            setcode { 'yay' }
+          end
+        }.should_not raise_error
+        @coll.value('yay').should be_nil
+      end
     end
   end
 

    

--
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