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.

Signed-off-by: Adrien Thebo <[email protected]>
---
Local-branch: ticket/master/7753
 lib/facter/util/collection.rb |   16 ++++++++++------
 lib/facter/util/fact.rb       |   19 ++++++++++++-------
 2 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/lib/facter/util/collection.rb b/lib/facter/util/collection.rb
index b3d3a45..8276840 100644
--- a/lib/facter/util/collection.rb
+++ b/lib/facter/util/collection.rb
@@ -33,13 +33,17 @@ class Facter::Util::Collection
 
         if block
             resolve = fact.add(&block)
-            # Set any resolve-appropriate options
-            options.each do |opt, value|
-                method = opt.to_s + "="
-                if resolve.respond_to?(method)
-                    resolve.send(method, value)
-                    options.delete(opt)
+            # If the resolve was actually added, set options
+            if resolve
+                options.each do |opt, value|
+                    method = opt.to_s + "="
+                    if resolve.respond_to?(method)
+                        resolve.send(method, value)
+                        options.delete(opt)
+                    end
                 end
+            else # Resolve failed to add, return what we have
+                return fact
             end
         end
 
diff --git a/lib/facter/util/fact.rb b/lib/facter/util/fact.rb
index 935b3c1..5796f2b 100644
--- a/lib/facter/util/fact.rb
+++ b/lib/facter/util/fact.rb
@@ -33,17 +33,22 @@ class Facter::Util::Fact
     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.debug "Unable to add resolve for #{@name}: #{e}"
+            nil
+        end
     end
 
     # Flush any cached values.
-- 
1.7.4.1

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