Skip to content

Commit

Permalink
add PluginDefinitionError to Ohai, rescue in loader.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
Claire McQuin committed Oct 29, 2013
1 parent 4a4aaba commit 5ed9d01
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
5 changes: 4 additions & 1 deletion lib/ohai/dsl/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def self.strict_const_defined?(const)
end
end

class PluginDefinitionError < Exception
end

def self.plugin(name, &block)
plugin = nil
if NamedPlugin.strict_const_defined?(name)
Expand Down Expand Up @@ -170,7 +173,7 @@ def self.depends(*attrs)
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
Expand Down
2 changes: 2 additions & 0 deletions lib/ohai/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def load_plugin(plugin_path, plugin_name=nil)
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
Expand Down
7 changes: 6 additions & 1 deletion lib/ohai/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ def load_plugins
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}")
Expand Down
4 changes: 2 additions & 2 deletions spec/ohai/dsl/plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
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
Expand All @@ -216,7 +216,7 @@
Ohai.plugin(@name) {
collect_data { }
}
}.to raise_error(Exception, /collect_data already defined/)
}.to raise_error(Ohai::PluginDefinitionError, /collect_data already defined/)
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/unit/loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@
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
Expand Down

0 comments on commit 5ed9d01

Please sign in to comment.