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 <adr...@puppetlabs.com> --- Local-branch: ticket/master/7753 lib/facter/util/fact.rb | 24 +++++++++++++----------- spec/unit/util/fact_spec.rb | 8 ++++++++ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/facter/util/fact.rb b/lib/facter/util/fact.rb index 935b3c1..587722e 100644 --- a/lib/facter/util/fact.rb +++ b/lib/facter/util/fact.rb @@ -33,17 +33,19 @@ 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) - - resolve.instance_eval(&block) - - @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 } - - return resolve + begin + resolve = Facter::Util::Resolution.new(@name) + resolve.instance_eval(&block) + @resolves << resolve + + # Immediately sort the resolutions, so that we always have + # a sorted list for looking up values. + # We always want to look them up in the order of number of + # confines, so the most restricted resolution always wins. + @resolves.sort! { |a, b| b.length <=> a.length } + rescue => e + Facter.debug "Unable to add resolve for #{@name}: #{e}" + end end # Flush any cached values. diff --git a/spec/unit/util/fact_spec.rb b/spec/unit/util/fact_spec.rb index 523c855..0c83bfd 100755 --- a/spec/unit/util/fact_spec.rb +++ b/spec/unit/util/fact_spec.rb @@ -126,4 +126,12 @@ describe Facter::Util::Fact do it "should have a method for flushing the cached fact" do Facter::Util::Fact.new(:foo).should respond_to(:flush) end + + it "should catch exceptions thrown when adding a fact to a resolve" do + fact = Facter::Util::Fact.new(:foo) + fact.add do + throw "should be caught" + end + fact.instance_variable_get("@resolves").size.should == 0 + end end -- 1.7.3.4 -- You received this message because you are subscribed to the Google Groups "Puppet Developers" group. To post to this group, send email to puppet-dev@googlegroups.com. To unsubscribe from this group, send email to puppet-dev+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.