Module: Puppet::Util::Errors

Overview

Some helper methods for throwing and populating errors.

Instance Method Summary collapse

Instance Method Details

#adderrorcontext(error, other = nil) ⇒ Exception

Add line and file info to the supplied exception if info is available from this object, is appropriately populated and the supplied exception supports it. When other is supplied, the backtrace will be copied to the error object and the ‘original’ will be dropped from the error.

Parameters:

  • error (Exception)

    exception that is populated with info

  • other (Exception) (defaults to: nil)

    original exception, source of backtrace info

Returns:

  • (Exception)

    error parameter



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/puppet/util/errors.rb', line 22

def adderrorcontext(error, other = nil)
  error.line ||= self.line if error.respond_to?(:line=) and self.respond_to?(:line) and self.line
  error.file ||= self.file if error.respond_to?(:file=) and self.respond_to?(:file) and self.file
  error.original ||= other if error.respond_to?(:original=)

  error.set_backtrace(other.backtrace) if other and other.respond_to?(:backtrace)
  # It is not meaningful to keep the wrapped exception since its backtrace has already
  # been adopted by the error. (The instance variable is private for good reasons).
  error.instance_variable_set(:@original, nil)
  error
end

#devfail(msg) ⇒ Object

Throw a Puppet::DevError with the specified message. Used for unknown or internal application failures.

Parameters:

  • msg (String)

    message used in raised error

Raises:



10
11
12
# File 'lib/puppet/util/errors.rb', line 10

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

#error_contextString

Return a human-readable string of this object’s file and line attributes, if set.

Returns:

  • (String)

    description of file and line



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/puppet/util/errors.rb', line 38

def error_context
  if file and line
    _(" at %{file}:%{line}") % { file: file, line: line }
  elsif line
    _(" at line %{line}") % { line: line }
  elsif file
    _(" in %{file}") % { file: 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.

Parameters:

  • options (Hash<Symbol,Object>) (defaults to: {})

    options used to create error

Options Hash (options):

  • :type (Class)

    error type to raise, defaults to Puppet::DevError

  • :message (String)

    message to use in error, default mentions the name of this class

Raises:

  • (Puppet::Error)

    re-raised with extra context if the block raises it

  • (Error)

    of type options, when the block raises other exceptions



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/puppet/util/errors.rb', line 61

def exceptwrap(options = {})
  options[:type] ||= Puppet::DevError
  begin
    return yield
  rescue Puppet::Error => detail
    raise adderrorcontext(detail)
  rescue => detail
    message = options[:message] || _("%{klass} failed with error %{error_type}: %{detail}") % { klass: self.class, error_type: detail.class, detail: 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(message, ..) ⇒ Object #fail(error_klass, message, ..) ⇒ Object #fail(error_klass, message, ..) ⇒ Object

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

Overloads:

  • #fail(message, ..) ⇒ Object

    Throw a Puppet::Error with a message concatenated from the given arguments.

    Parameters:

    • message (String)

      error message(s)

  • #fail(error_klass, message, ..) ⇒ Object

    Throw an exception of type error_klass with a message concatenated from the given arguments.

    Parameters:

    • type (Class)

      of error

    • message (String)

      error message(s)

  • #fail(error_klass, message, ..) ⇒ Object

    Throw an exception of type error_klass with a message concatenated from the given arguments.

    Parameters:

    • type (Class)

      of error

    • message (String)

      error message(s)

    • original (Exception)

      exception, source of backtrace info



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/puppet/util/errors.rb', line 96

def fail(*args)
  if args[0].is_a?(Class)
    type = args.shift
  else
    type = Puppet::Error
  end

  other = args.count > 1 ? args.pop : nil
  error = adderrorcontext(type.new(args.join(" ")), other)

  raise error
end