Sha256: af5b6c9424ffb6b04a8f536122cac7b096ee30d1e26e0b29ae94f43749f9b610

Contents?: true

Size: 1.5 KB

Versions: 63

Compression:

Stored size: 1.5 KB

Contents

# 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

    error.set_backtrace other.backtrace if other and other.respond_to?(:backtrace)

    error
  end

  def error_context
    if file and line
      " at #{file}:#{line}"
    elsif line
      " at line #{line}"
    elsif file
      " in #{file}"
    else
      ""
    end
  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
      return yield
    rescue Puppet::Error => detail
      raise adderrorcontext(detail)
    rescue => detail
      message = options[:message] || "#{self.class} failed with error #{detail.class}: #{detail}"

      error = options[:type].new(message)
      # We can't use self.fail here because it always expects strings,
      # not exceptions.
      raise adderrorcontext(error, detail)
    end

    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

Version data entries

63 entries across 63 versions & 4 rubygems

Version Path
puppet-parse-0.1.4 lib/vendor/puppet/util/errors.rb
puppet-parse-0.1.3 lib/vendor/puppet/util/errors.rb
puppet-parse-0.1.2 lib/vendor/puppet/util/errors.rb
puppet-parse-0.1.1 lib/vendor/puppet/util/errors.rb
puppet-2.7.26 lib/puppet/util/errors.rb
puppet-2.7.25 lib/puppet/util/errors.rb
puppet-2.7.24 lib/puppet/util/errors.rb
puppet-2.7.23 lib/puppet/util/errors.rb
puppet-2.7.22 lib/puppet/util/errors.rb
puppet-parse-0.1.0 lib/vendor/puppet/util/errors.rb
puppet-parse-0.0.6 lib/vendor/puppet/util/errors.rb
puppet-2.7.21 lib/puppet/util/errors.rb
puppet-2.6.18 lib/puppet/util/errors.rb
puppet-parse-0.0.5 lib/vendor/puppet/util/errors.rb
puppet-parse-0.0.4 lib/vendor/puppet/util/errors.rb
puppet-parse-0.0.2 lib/vendor/puppet/util/errors.rb
puppet-2.7.20 lib/puppet/util/errors.rb
puppet-2.7.20.rc1 lib/puppet/util/errors.rb
librarian-puppet-0.9.4 vendor/gems/ruby/1.8/gems/puppet-2.7.18/lib/puppet/util/errors.rb
puppet-2.7.19 lib/puppet/util/errors.rb