diff options
author | Claire McQuin <claire@opscode.com> | 2013-10-24 12:19:27 -0700 |
---|---|---|
committer | Claire McQuin <claire@opscode.com> | 2013-10-29 16:12:36 -0700 |
commit | 5ed9d0126414e7d7ffb5973c53dffb017043493f (patch) | |
tree | 05dc9989a8afd259f90af83cb04357523b0df596 | |
parent | 4a4aaba40bb290413f96e3f2207687832ecb399a (diff) | |
download | ohai-5ed9d0126414e7d7ffb5973c53dffb017043493f.tar.gz |
add PluginDefinitionError to Ohai, rescue in loader.rb
-rw-r--r-- | lib/ohai/dsl/plugin.rb | 5 | ||||
-rw-r--r-- | lib/ohai/loader.rb | 2 | ||||
-rw-r--r-- | lib/ohai/system.rb | 7 | ||||
-rw-r--r-- | spec/ohai/dsl/plugin_spec.rb | 4 | ||||
-rw-r--r-- | spec/unit/loader_spec.rb | 14 |
5 files changed, 28 insertions, 4 deletions
diff --git a/lib/ohai/dsl/plugin.rb b/lib/ohai/dsl/plugin.rb index eb3096e7..72a0493c 100644 --- a/lib/ohai/dsl/plugin.rb +++ b/lib/ohai/dsl/plugin.rb @@ -38,6 +38,9 @@ module Ohai end end + class PluginDefinitionError < Exception + end + def self.plugin(name, &block) plugin = nil if NamedPlugin.strict_const_defined?(name) @@ -170,7 +173,7 @@ module Ohai def self.collect_data(platform = :default, *other_platforms, &block) [platform, other_platforms].flatten.each do |plat| if data_collector.has_key?(plat) - raise Exception, "collect_data already defined on platform #{plat}" + raise Ohai::PluginDefinitionError, "collect_data already defined on platform #{plat}" else data_collector[plat] = block end diff --git a/lib/ohai/loader.rb b/lib/ohai/loader.rb index f0ff6232..e450a936 100644 --- a/lib/ohai/loader.rb +++ b/lib/ohai/loader.rb @@ -47,6 +47,8 @@ module Ohai plugin = klass.new(@controller, plugin_path) unless klass.nil? rescue SystemExit, Interrupt raise + rescue PluginDefinitionError => e + Ohai::Log.warn("Plugin at #{plugin_path} is not properly defined: #{e.message}") rescue NoMethodError => e Ohai::Log.warn("[UNSUPPORTED OPERATION] Plugin at #{plugin_path} used unsupported operation \'#{e.name.to_s}\'") rescue Exception, Errno::ENOENT => e diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb index d842cd0b..c724a4e7 100644 --- a/lib/ohai/system.rb +++ b/lib/ohai/system.rb @@ -70,7 +70,12 @@ module Ohai if md plugin_name = md[1].gsub(File::SEPARATOR, "::") unless @v6_dependency_solver.has_key?(plugin_name) - plugin = @loader.load_plugin(file, plugin_name) + begin + plugin = @loader.load_plugin(file, plugin_name) + rescue PluginDefinitionError => e + Ohai::Log.error("Encountered error while loading plugin #{file}: #{e.inspect}") + raise + end @v6_dependency_solver[plugin_name] = plugin unless plugin.nil? else Ohai::Log.debug("Already loaded plugin at #{file}") diff --git a/spec/ohai/dsl/plugin_spec.rb b/spec/ohai/dsl/plugin_spec.rb index 2308d9e2..64b87c83 100644 --- a/spec/ohai/dsl/plugin_spec.rb +++ b/spec/ohai/dsl/plugin_spec.rb @@ -207,7 +207,7 @@ describe Ohai::DSL::Plugin::VersionVII do collect_data { } collect_data { } } - }.to raise_error(Exception, /collect_data already defined/) + }.to raise_error(Ohai::PluginDefinitionError, /collect_data already defined/) end it "should fail if a platform has already been defined in another plugin file" do @@ -216,7 +216,7 @@ describe Ohai::DSL::Plugin::VersionVII do Ohai.plugin(@name) { collect_data { } } - }.to raise_error(Exception, /collect_data already defined/) + }.to raise_error(Ohai::PluginDefinitionError, /collect_data already defined/) end end diff --git a/spec/unit/loader_spec.rb b/spec/unit/loader_spec.rb index 02bf41c7..4d640003 100644 --- a/spec/unit/loader_spec.rb +++ b/spec/unit/loader_spec.rb @@ -68,6 +68,20 @@ EOF plugin.version.should eql(:version7) end + it "should log a warning from PluginDefinitionError when plugin poorly defined" do + contents = <<EOF +Ohai.plugin(:#{@name}) do + collect_data(:darwin) do + end + collect_data(:darwin) do + end +end +EOF + IO.stub(:read).with(@path).and_return(contents) + Ohai::Log.should_receive(:warn).with(/is not properly defined/) + @loader.load_plugin(@path) + end + it "should log a warning from NoMethodError when plugin uses a non dsl command" do contents = <<EOF Ohai.plugin(:#{@name}) do |