module Puppet::Util::Errors

Some helper methods for throwing and populating errors.

@api public

Public Class Methods

error_location(file, line=nil, column=nil) click to toggle source

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

@param file [String] the file path for the error (nil or “”, for not known) @param line [String] the line number for the error (nil or “”, for not known) @param column [String] the column number for the error (nil or “”, for not known) @return [String] description of file, line, and column

   # File lib/puppet/util/errors.rb
43 def self.error_location(file, line=nil, column=nil)
44   file = nil if (file.is_a?(String) && file.empty?)
45   line = nil if (line.is_a?(String) && line.empty?)
46   column = nil if (column.is_a?(String) && column.empty?)
47   if file and line and column
48     _("(file: %{file}, line: %{line}, column: %{column})") % { file: file, line: line, column: column }
49   elsif file and line
50     _("(file: %{file}, line: %{line})") % { file: file, line: line }
51   elsif line and column
52     _("(line: %{line}, column: %{column})") % { line: line, column: column }
53   elsif line
54     _("(line: %{line})") % { line: line }
55   elsif file
56     _("(file: %{file})") % { file: file }
57   else
58     ''
59   end
60 end
error_location_with_space(file, line=nil, column=nil) click to toggle source

Return a human-readable string of this object's file, line, and pos attributes, with a proceeding space in the output if set.

@param file [String] the file path for the error (nil or “”, for not known) @param line [String] the line number for the error (nil or “”, for not known) @param column [String] the column number for the error (nil or “”, for not known) @return [String] description of file, line, and column

   # File lib/puppet/util/errors.rb
71 def self.error_location_with_space(file, line=nil, column=nil)
72   error_location_str = error_location(file, line, column)
73   if error_location_str.empty?
74     ''
75   else
76     ' ' + error_location_str
77   end
78 end
error_location_with_unknowns(file, line) click to toggle source

Return a human-readable string of this object's file and line where unknown entries are listed as 'unknown'

@param file [String] the file path for the error (nil or “”, for not known) @param line [String] the line number for the error (nil or “”, for not known) @return [String] description of file, and line

   # File lib/puppet/util/errors.rb
86 def self.error_location_with_unknowns(file, line)
87   file = nil if (file.is_a?(String) && file.empty?)
88   line = nil if (line.is_a?(String) && line.empty?)
89   file = _('unknown') unless file
90   line = _('unknown') unless line
91   error_location(file, line)
92 end

Public Instance Methods

adderrorcontext(error, other = nil) click to toggle source

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.

@param error [Exception] exception that is populated with info @param other [Exception] original exception, source of backtrace info @return [Exception] error parameter

   # File lib/puppet/util/errors.rb
23 def adderrorcontext(error, other = nil)
24   error.line ||= self.line if error.respond_to?(:line=) and self.respond_to?(:line) and self.line
25   error.file ||= self.file if error.respond_to?(:file=) and self.respond_to?(:file) and self.file
26   error.original ||= other if error.respond_to?(:original=)
27 
28   error.set_backtrace(other.backtrace) if other and other.respond_to?(:backtrace)
29   # It is not meaningful to keep the wrapped exception since its backtrace has already
30   # been adopted by the error. (The instance variable is private for good reasons).
31   error.instance_variable_set(:@original, nil)
32   error
33 end
devfail(msg) click to toggle source

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

@param msg [String] message used in raised error @raise [Puppet::DevError] always raised with the supplied message

   # File lib/puppet/util/errors.rb
11 def devfail(msg)
12   self.fail(Puppet::DevError, msg)
13 end
error_context() click to toggle source

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

@return [String] description of file and line with a leading space

    # File lib/puppet/util/errors.rb
 98 def error_context
 99   Puppet::Util::Errors.error_location_with_space(file, line)
100 end
exceptwrap(options = {}) { || ... } click to toggle source

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

@param options [Hash<Symbol,Object>] options used to create error @option options [Class] :type error type to raise, defaults to

Puppet::DevError

@option options [String] :message message to use in error, default mentions

the name of this class

@raise [Puppet::Error] re-raised with extra context if the block raises it @raise [Error] of type options, when the block raises other

exceptions
    # File lib/puppet/util/errors.rb
113 def exceptwrap(options = {})
114   options[:type] ||= Puppet::DevError
115   begin
116     return yield
117   rescue Puppet::Error => detail
118     raise adderrorcontext(detail)
119   rescue => detail
120     message = options[:message] || _("%{klass} failed with error %{error_type}: %{detail}") % { klass: self.class, error_type: detail.class, detail: detail }
121 
122     error = options[:type].new(message)
123     # We can't use self.fail here because it always expects strings,
124     # not exceptions.
125     raise adderrorcontext(error, detail)
126   end
127 
128   retval
129 end
fail(*args) click to toggle source

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

@overload fail(message, ..)

Throw a Puppet::Error with a message concatenated from the given
arguments.
@param [String] message error message(s)

@overload fail(error_klass, message, ..)

Throw an exception of type error_klass with a message concatenated from
the given arguments.
@param [Class] type of error
@param [String] message error message(s)

@overload fail(error_klass, message, ..)

Throw an exception of type error_klass with a message concatenated from
the given arguments.
@param [Class] type of error
@param [String] message error message(s)
@param [Exception] original exception, source of backtrace info
    # File lib/puppet/util/errors.rb
148 def fail(*args)
149   if args[0].is_a?(Class)
150     type = args.shift
151   else
152     type = Puppet::Error
153   end
154 
155   other = args.count > 1 ? args.pop : nil
156   error = adderrorcontext(type.new(args.join(" ")), other)
157 
158   raise error
159 end