Sha256: bc7c6793ab6a7db533c316007f9df7e41fa7d63be06543843a89ee2ad7c178c4

Contents?: true

Size: 1.53 KB

Versions: 16

Compression:

Stored size: 1.53 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

        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
            return 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

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
puppet-0.25.5 lib/puppet/util/errors.rb
puppet-0.25.4 lib/puppet/util/errors.rb
puppet-0.25.3 lib/puppet/util/errors.rb
puppet-0.24.9 lib/puppet/util/errors.rb
puppet-0.25.2 lib/puppet/util/errors.rb
puppet-0.25.1 lib/puppet/util/errors.rb
puppet-0.25.0 lib/puppet/util/errors.rb
puppet-0.24.0 lib/puppet/util/errors.rb
puppet-0.24.3 lib/puppet/util/errors.rb
puppet-0.24.2 lib/puppet/util/errors.rb
puppet-0.24.4 lib/puppet/util/errors.rb
puppet-0.24.1 lib/puppet/util/errors.rb
puppet-0.24.7 lib/puppet/util/errors.rb
puppet-0.24.5 lib/puppet/util/errors.rb
puppet-0.24.6 lib/puppet/util/errors.rb
puppet-0.24.8 lib/puppet/util/errors.rb