Module: Puppet::Util::Errors

Included in:
Parameter, Parser::AST, Parser::Compiler, Parser::Resource, Parser::Resource::Param, Parser::Scope, Provider, Resource::Type, Type
Defined in:
lib/vendor/puppet/util/errors.rb

Overview

Some helper methods for throwing errors.

Instance Method Summary collapse

Instance Method Details

#adderrorcontext(error, other = nil) ⇒ Object

Add line and file info if available and appropriate.



9
10
11
12
13
14
15
16
# File 'lib/vendor/puppet/util/errors.rb', line 9

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

#devfail(msg) ⇒ Object

Throw a dev error.



4
5
6
# File 'lib/vendor/puppet/util/errors.rb', line 4

def devfail(msg)
  self.fail(Puppet::DevError, msg)
end

#error_contextObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vendor/puppet/util/errors.rb', line 18

def error_context
  if file and line
    " at #{file}:#{line}"
  elsif line
    " at line #{line}"
  elsif file
    " in #{file}"
  else
    ""
  end
end

#exceptwrap(options = {}) ⇒ Object

Wrap a call in such a way that we always throw the right exception and keep as much context as possible.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vendor/puppet/util/errors.rb', line 32

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

#fail(*args) ⇒ Object

Throw an error, defaulting to a Puppet::Error.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vendor/puppet/util/errors.rb', line 51

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