Skip to content

Commit

Permalink
add exceptions to ohai/exception.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
Claire McQuin committed Oct 29, 2013
1 parent 5ed9d01 commit 4418bc0
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 36 deletions.
2 changes: 1 addition & 1 deletion lib/ohai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
require 'ohai/version'
require 'ohai/config'
require 'ohai/system'

require 'ohai/exception'
5 changes: 1 addition & 4 deletions lib/ohai/dsl/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ 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 @@ -173,7 +170,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 Ohai::PluginDefinitionError, "collect_data already defined on platform #{plat}"
raise Ohai::Exceptions::IllegalPluginDefinition, "collect_data already defined on platform #{plat}"
else
data_collector[plat] = block
end
Expand Down
3 changes: 3 additions & 0 deletions lib/ohai/exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
module Ohai
module Exceptions
class Exec < RuntimeError; end
class IllegalPluginDefinition < Exception; end
class AttributeNotFound < Exception; end
class DependencyCycle < Exception; end
end
end
4 changes: 2 additions & 2 deletions lib/ohai/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +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 Ohai::Exceptions::IllegalPluginDefinition => e
Ohai::Log.warn("Plugin at #{plugin_path} is not properly defined: #{e.inspect}")
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
11 changes: 3 additions & 8 deletions lib/ohai/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@
require 'ohai/dsl/plugin'

module Ohai
class NoAttributeError < Exception
end

class DependencyCycleError < Exception
end

class Runner
# safe_run: set to true if this runner will run plugins in
# safe-mode. default false.
Expand All @@ -45,7 +39,7 @@ def run_plugin(plugin, force = false)
next if p.has_run? unless force

if visited.include?(p)
raise DependencyCycleError, "Dependency cycle detected. Please refer to the following plugins: #{get_cycle(visited, p).join(", ") }"
raise Ohai::Exceptions::DependencyCycle, "Dependency cycle detected. Please refer to the following plugins: #{get_cycle(visited, p).join(", ") }"
end

dependency_providers = fetch_plugins(p.dependencies)
Expand All @@ -66,7 +60,8 @@ def fetch_plugins(attributes)
attrs = @attributes
parts = attribute.split('/')
parts.each do |part|
raise NoAttributeError, "Cannot find plugin providing attribute \'#{attribute}\'" unless attrs[part]
next if part == Ohai::Mixin::OS.collect_os
raise Ohai::Exceptions::AttributeNotFound, "Cannot find plugin providing attribute \'#{attribute}\'" unless attrs[part]
attrs = attrs[part]
end
plugins << attrs[:_plugins]
Expand Down
9 changes: 2 additions & 7 deletions lib/ohai/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,7 @@ def load_plugins
if md
plugin_name = md[1].gsub(File::SEPARATOR, "::")
unless @v6_dependency_solver.has_key?(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
plugin = @loader.load_plugin(file, plugin_name)
@v6_dependency_solver[plugin_name] = plugin unless plugin.nil?
else
Ohai::Log.debug("Already loaded plugin at #{file}")
Expand All @@ -100,7 +95,7 @@ def run_plugins(safe = false, force = false)
plugins = collect_plugins(@attributes)
begin
plugins.each { |plugin| @runner.run_plugin(plugin, force) }
rescue DependencyCycleError, NoAttributeError => e
rescue Ohai::Exceptions::AttributeNotFound, Ohai::Exceptions::DependencyCycle => e
Ohai::Log.error("Encountered error while running plugins: #{e.inspect}")
raise
end
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(Ohai::PluginDefinitionError, /collect_data already defined/)
}.to raise_error(Ohai::Exceptions::IllegalPluginDefinition, /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(Ohai::PluginDefinitionError, /collect_data already defined/)
}.to raise_error(Ohai::Exceptions::IllegalPluginDefinition, /collect_data already defined/)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/unit/loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
plugin.version.should eql(:version7)
end

it "should log a warning from PluginDefinitionError when plugin poorly defined" do
it "should log a warning when plugin poorly defined" do
contents = <<EOF
Ohai.plugin(:#{@name}) do
collect_data(:darwin) do
Expand Down
14 changes: 7 additions & 7 deletions spec/unit/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@
@plugin = klass.new(@ohai, "/tmp/plugins/thing.rb")
end

it "should raise a NoAttributeError" do
expect { @runner.run_plugin(@plugin) }.to raise_error(Ohai::NoAttributeError)
it "should raise Ohai::Excpetions::AttributeNotFound" do
expect { @runner.run_plugin(@plugin) }.to raise_error(Ohai::Exceptions::AttributeNotFound)
end

it "should not run the plugin" do
expect { @runner.run_plugin(@plugin) }.to raise_error(Ohai::NoAttributeError)
expect { @runner.run_plugin(@plugin) }.to raise_error(Ohai::Exceptions::AttributeNotFound)
@plugin.has_run?.should be_false
end
end
Expand Down Expand Up @@ -251,10 +251,10 @@
@plugin1, @plugin2 = @plugins
end

it "should raise a DependencyCycleError" do
@runner.stub(:fetch_plugins).with(["thing"]).and_return([@plugin1])
@runner.stub(:fetch_plugins).with(["other"]).and_return([@plugin2])
expect { @runner.run_plugin(@plugin1) }.to raise_error(Ohai::DependencyCycleError)
it "should raise Ohai::Exceptions::DependencyCycle" do
@runner.stub(:fetch_providers).with(["thing"]).and_return([@plugin1])
@runner.stub(:fetch_providers).with(["other"]).and_return([@plugin2])
expect { @runner.run_plugin(@plugin1) }.to raise_error(Ohai::Exceptions::DependencyCycle)
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/unit/system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@
@ohai.stub(:collect_plugins).and_return([plugin])
end

describe "when a NoAttributeError is received" do
describe "when AttributeNotFound is received" do
it "should write an error to Ohai::Log" do
@runner.stub(:run_plugin).and_raise(Ohai::NoAttributeError)
Ohai::Log.should_receive(:error).with(/NoAttributeError/)
expect { @ohai.run_plugins }.to raise_error(Ohai::NoAttributeError)
@runner.stub(:run_plugin).and_raise(Ohai::Exceptions::AttributeNotFound)
Ohai::Log.should_receive(:error).with(/Ohai::Exceptions::AttributeNotFound/)
expect { @ohai.run_plugins }.to raise_error(Ohai::Exceptions::AttributeNotFound)
end
end
end
Expand Down

0 comments on commit 4418bc0

Please sign in to comment.