Sha256: 8781b388154a8a31f1abbf034b5fb13392502a15c162e6251e709a1f05e29080

Contents?: true

Size: 1.78 KB

Versions: 21

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

# Catches errors in a given block and returns them. This will return the
# output of the block if no errors are raised. Accepts an optional list of
# error kinds to catch.
#
# > **Note:** Not available in apply block
Puppet::Functions.create_function(:catch_errors) do
  # @param error_types An array of error types to catch
  # @param block The block of steps to catch errors on
  # @return If an error is raised in the block then the error will be returned,
  #         otherwise the result will be returned
  # @example Catch errors for a block
  #   catch_errors() || {
  #     run_command("whoami", $targets)
  #     run_command("adduser ryan", $targets)
  #   }
  # @example Catch parse errors for a block of code
  #   catch_errors(['bolt/parse-error']) || {
  #    run_plan('canary', $targets)
  #    run_plan('other_plan)
  #    apply($targets) || {
  #      notify { "Hello": }
  #    }
  #   }
  dispatch :catch_errors do
    optional_param 'Array[String[1]]', :error_types
    block_param 'Callable[0, 0]', :block
    return_type 'Any'
  end

  def catch_errors(error_types = nil)
    unless Puppet[:tasks]
      raise Puppet::ParseErrorWithIssue
        .from_issue_and_stack(Bolt::PAL::Issues::PLAN_OPERATION_NOT_SUPPORTED_WHEN_COMPILING,
                              action: self.class.name)
    end

    executor = Puppet.lookup(:bolt_executor)
    # Send Analytics Report
    executor.report_function_call(self.class.name)

    begin
      yield
    rescue Puppet::PreformattedError => e
      if e.cause.is_a?(Bolt::Error)
        if error_types.nil?
          e.cause.to_puppet_error
        elsif error_types.include?(e.cause.to_h['kind'])
          e.cause.to_puppet_error
        else
          raise e
        end
      else
        raise e
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
bolt-2.34.0 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb
bolt-2.33.2 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb
bolt-2.33.1 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb
bolt-2.32.0 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb
bolt-2.31.0 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb
bolt-2.30.0 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb
bolt-2.29.0 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb
bolt-2.28.0 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb
bolt-2.27.0 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb
bolt-2.26.0 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb
bolt-2.25.0 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb
bolt-2.24.1 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb
bolt-2.24.0 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb
bolt-2.23.0 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb
bolt-2.22.0 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb
bolt-2.21.0 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb
bolt-2.20.0 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb
bolt-2.19.0 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb
bolt-2.18.0 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb
bolt-2.17.0 bolt-modules/boltlib/lib/puppet/functions/catch_errors.rb