diff options
author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-10-04 18:24:24 +0000 |
---|---|---|
committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-10-04 18:24:24 +0000 |
commit | 28cee40689440388994a4768bd301ae32c8ecc05 (patch) | |
tree | c865ab44f4c9247052cf83de16ffc8ebe8b15e54 /lib/puppet/util | |
parent | e0e291332bd4676962a28c7b220ae5c5e9651c0a (diff) | |
download | puppet-28cee40689440388994a4768bd301ae32c8ecc05.tar.gz puppet-28cee40689440388994a4768bd301ae32c8ecc05.tar.xz puppet-28cee40689440388994a4768bd301ae32c8ecc05.zip |
Merging the changes from the override-refactor branch. This is a significant rewrite of the parser, but it has little affect on the rest of the code tree.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1726 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/puppet/util')
-rw-r--r-- | lib/puppet/util/errors.rb | 55 | ||||
-rw-r--r-- | lib/puppet/util/logging.rb | 20 | ||||
-rw-r--r-- | lib/puppet/util/methodhelper.rb | 21 |
3 files changed, 96 insertions, 0 deletions
diff --git a/lib/puppet/util/errors.rb b/lib/puppet/util/errors.rb new file mode 100644 index 000000000..ad129ee34 --- /dev/null +++ b/lib/puppet/util/errors.rb @@ -0,0 +1,55 @@ +# Some helper methods for throwing errors. +module Puppet::Util::Errors + # Throw a dev error. + def devfail(msg) + self.fail(Puppet::DevError, msg) + end + + # Add line and file info if available and appropriate. + def adderrorcontext(error, other = nil) + error.line ||= self.line if self.respond_to?(:line) and self.line + error.file ||= self.file if self.respond_to?(:file) and self.file + + if other and other.respond_to?(:backtrace) + error.set_backtrace other.backtrace + end + + return error + end + + # Wrap a call in such a way that we always throw the right exception and keep + # as much context as possible. + def exceptwrap(options = {}) + options[:type] ||= Puppet::DevError + begin + retval = yield + rescue Puppet::Error => detail + raise adderrorcontext(detail) + rescue => detail + message = options[:message] || "%s failed with error %s: %s" % + [self.class, detail.class, detail.to_s] + + error = options[:type].new(message) + # We can't use self.fail here because it always expects strings, + # not exceptions. + raise adderrorcontext(error, detail) + end + + return retval + end + + # Throw an error, defaulting to a Puppet::Error. + def fail(*args) + if args[0].is_a?(Class) + type = args.shift + else + type = Puppet::Error + end + + error = adderrorcontext(type.new(args.join(" "))) + + raise error + end +end + +# $Id$ diff --git a/lib/puppet/util/logging.rb b/lib/puppet/util/logging.rb new file mode 100644 index 000000000..1245e24de --- /dev/null +++ b/lib/puppet/util/logging.rb @@ -0,0 +1,20 @@ +# A module to make logging a bit easier. +require 'puppet/log' + +module Puppet::Util::Logging + # Create a method for each log level. + Puppet::Log.eachlevel do |level| + define_method(level) do |args| + if args.is_a?(Array) + args = args.join(" ") + end + Puppet::Log.create( + :level => level, + :source => self, + :message => args + ) + end + end +end + +# $Id$ diff --git a/lib/puppet/util/methodhelper.rb b/lib/puppet/util/methodhelper.rb index 5643ac245..c229e8efb 100644 --- a/lib/puppet/util/methodhelper.rb +++ b/lib/puppet/util/methodhelper.rb @@ -1,5 +1,26 @@ # Where we store helper methods related to, um, methods. module Puppet::Util::MethodHelper + def requiredopts(*names) + names.each do |name| + if self.send(name).nil? + devfail("%s is a required option for %s" % [name, self.class]) + end + end + end + + # Iterate over a hash, treating each member as an attribute. + def set_options(options) + options.dup.each do |param,value| + method = param.to_s + "=" + unless self.respond_to?(method) + self.fail "Invalid parameter %s to object class %s" % + [param,self.class.to_s] + end + + self.send(method,value) + end + end + # Take a hash and convert all of the keys to symbols if possible. def symbolize_options(options) options.inject({}) do |hash, opts| |