Method: Puppet::Util::Errors#exceptwrap

Defined in:
lib/puppet/util/errors.rb

#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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/puppet/util/errors.rb', line 112

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