summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaire McQuin <claire@opscode.com>2013-10-24 12:31:01 -0700
committerClaire McQuin <claire@opscode.com>2013-10-29 16:12:36 -0700
commit4418bc0455fae1649037f4925acde1899743db77 (patch)
treeb1d6951deda51b1c54b15caf1291cbb8670eb975
parent5ed9d0126414e7d7ffb5973c53dffb017043493f (diff)
downloadohai-4418bc0455fae1649037f4925acde1899743db77.tar.gz
add exceptions to ohai/exception.rb
-rw-r--r--lib/ohai.rb2
-rw-r--r--lib/ohai/dsl/plugin.rb5
-rw-r--r--lib/ohai/exception.rb3
-rw-r--r--lib/ohai/loader.rb4
-rw-r--r--lib/ohai/runner.rb11
-rw-r--r--lib/ohai/system.rb9
-rw-r--r--spec/ohai/dsl/plugin_spec.rb4
-rw-r--r--spec/unit/loader_spec.rb2
-rw-r--r--spec/unit/runner_spec.rb14
-rw-r--r--spec/unit/system_spec.rb8
10 files changed, 26 insertions, 36 deletions
diff --git a/lib/ohai.rb b/lib/ohai.rb
index d86549ab..1d80ed64 100644
--- a/lib/ohai.rb
+++ b/lib/ohai.rb
@@ -19,4 +19,4 @@
require 'ohai/version'
require 'ohai/config'
require 'ohai/system'
-
+require 'ohai/exception'
diff --git a/lib/ohai/dsl/plugin.rb b/lib/ohai/dsl/plugin.rb
index 72a0493c..023f511a 100644
--- a/lib/ohai/dsl/plugin.rb
+++ b/lib/ohai/dsl/plugin.rb
@@ -38,9 +38,6 @@ module Ohai
end
end
- class PluginDefinitionError < Exception
- end
-
def self.plugin(name, &block)
plugin = nil
if NamedPlugin.strict_const_defined?(name)
@@ -173,7 +170,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 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
diff --git a/lib/ohai/exception.rb b/lib/ohai/exception.rb
index 228dbca2..62af2841 100644
--- a/lib/ohai/exception.rb
+++ b/lib/ohai/exception.rb
@@ -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
diff --git a/lib/ohai/loader.rb b/lib/ohai/loader.rb
index e450a936..6428f969 100644
--- a/lib/ohai/loader.rb
+++ b/lib/ohai/loader.rb
@@ -47,8 +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 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
diff --git a/lib/ohai/runner.rb b/lib/ohai/runner.rb
index 67930c1c..c16f0f54 100644
--- a/lib/ohai/runner.rb
+++ b/lib/ohai/runner.rb
@@ -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.
@@ -45,7 +39,7 @@ module Ohai
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)
@@ -66,7 +60,8 @@ module Ohai
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]
diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb
index c724a4e7..02d41387 100644
--- a/lib/ohai/system.rb
+++ b/lib/ohai/system.rb
@@ -70,12 +70,7 @@ module Ohai
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}")
@@ -100,7 +95,7 @@ module Ohai
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
diff --git a/spec/ohai/dsl/plugin_spec.rb b/spec/ohai/dsl/plugin_spec.rb
index 64b87c83..e51301ed 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(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
@@ -216,7 +216,7 @@ describe Ohai::DSL::Plugin::VersionVII do
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
diff --git a/spec/unit/loader_spec.rb b/spec/unit/loader_spec.rb
index 4d640003..49b0678f 100644
--- a/spec/unit/loader_spec.rb
+++ b/spec/unit/loader_spec.rb
@@ -68,7 +68,7 @@ EOF
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
diff --git a/spec/unit/runner_spec.rb b/spec/unit/runner_spec.rb
index fa621cc3..669d905c 100644
--- a/spec/unit/runner_spec.rb
+++ b/spec/unit/runner_spec.rb
@@ -69,12 +69,12 @@ describe Ohai::Runner, "run_plugin" do
@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
@@ -251,10 +251,10 @@ describe Ohai::Runner, "run_plugin" do
@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
diff --git a/spec/unit/system_spec.rb b/spec/unit/system_spec.rb
index dd93a1e9..0cdfab52 100644
--- a/spec/unit/system_spec.rb
+++ b/spec/unit/system_spec.rb
@@ -131,11 +131,11 @@ describe "Ohai::System" do
@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