Skip to content

Commit

Permalink
fix specs for exception handling
Browse files Browse the repository at this point in the history
- setup a class heirarchy of exceptions so i can easily rethrow
  any internal ohai error
- catch all the non-internal errors from running the plugin and
  just warn on those
- convert the argumenterror raises to ohai invalidplugin errors
  • Loading branch information
lamont-granquist committed Jan 7, 2014
1 parent daec3dc commit ae86c3f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
20 changes: 11 additions & 9 deletions lib/ohai/exception.rb
Expand Up @@ -6,9 +6,9 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -19,12 +19,14 @@
module Ohai
module Exceptions
class Exec < RuntimeError; end
class InvalidPluginName < Exception; end
class IllegalPluginDefinition < Exception; end
class AttributeNotFound < Exception; end
class ProviderNotFound < Exception; end
class DependencyCycle < Exception; end
class DependencyNotFound < Exception; end
class AttributeSyntaxError < Exception; end
class Error < Exception; end
class InvalidPlugin < Error; end
class InvalidPluginName < Error; end
class IllegalPluginDefinition < Error; end
class AttributeNotFound < Error; end
class ProviderNotFound < Error; end
class DependencyCycle < Error; end
class DependencyNotFound < Error; end
class AttributeSyntaxError < Error; end
end
end
6 changes: 3 additions & 3 deletions lib/ohai/runner.rb
Expand Up @@ -34,7 +34,7 @@ def initialize(controller, safe_run = false)
# will be run even if they have been run before.
def run_plugin(plugin, force = false)
unless plugin.kind_of?(Ohai::DSL::Plugin)
raise ArgumentError, "Invalid plugin #{plugin} (must be an Ohai::DSL::Plugin or subclass)"
raise Ohai::Exceptions::InvalidPlugin, "Invalid plugin #{plugin} (must be an Ohai::DSL::Plugin or subclass)"
end

if Ohai::Config[:disabled_plugins].include?(plugin.name)
Expand All @@ -49,9 +49,9 @@ def run_plugin(plugin, force = false)
when :version6
run_v6_plugin(plugin, force)
else
raise ArgumentError, "Invalid plugin version #{plugin.version} for plugin #{plugin}"
raise Ohai::Exceptions::InvalidPlugin, "Invalid plugin version #{plugin.version} for plugin #{plugin}"
end
rescue Ohai::Exceptions::AttributeNotFound, Ohai::Exceptions::DependencyCycle
rescue Ohai::Exceptions::Error
raise
rescue Exception,Errno::ENOENT => e
Ohai::Log.debug("Plugin #{plugin.name} threw exception #{e.inspect} #{e.backtrace.join("\n")}")
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/runner_spec.rb
Expand Up @@ -29,7 +29,7 @@

describe "when running an invalid plugin" do
it "should raise error" do
lambda { @runner.run_plugin(double("Ohai::NotPlugin")) }.should raise_error(ArgumentError)
lambda { @runner.run_plugin(double("Ohai::NotPlugin")) }.should raise_error(Ohai::Exceptions::InvalidPlugin)
end
end

Expand Down Expand Up @@ -120,7 +120,7 @@
let(:version) { :versionBla }

it "should raise error" do
lambda { @runner.run_plugin(plugin) }.should raise_error(ArgumentError)
lambda { @runner.run_plugin(plugin) }.should raise_error(Ohai::Exceptions::InvalidPlugin)
end
end

Expand Down

0 comments on commit ae86c3f

Please sign in to comment.