Sha256: 50bcff066126ad05bae3079a2cd1226cf2c1068aca60a20d2f5fdc10eeaddeef

Contents?: true

Size: 1.79 KB

Versions: 8

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

require 'bolt/error'

# Raises a Bolt::PlanFailure exception to signal to callers that the plan failed.
#
# Plan authors should call this function when their plan is not successful. The
# error may then be caught by another plans run_plan function or in bolt itself
#
# **NOTE:** Not available in apply block
Puppet::Functions.create_function(:fail_plan) do
  # Fail a plan, generating an exception from the parameters.
  # @param msg An error message.
  # @param kind An easily matchable error kind.
  # @param details Machine-parseable details about the error.
  # @param issue_code Unused.
  # @return Raises an exception.
  # @example Raise an exception
  #   fail_plan('We goofed up', 'task-unexpected-result', { 'result' => 'null' })
  dispatch :from_args do
    param 'String[1]', :msg
    optional_param 'String[1]', :kind
    optional_param 'Hash[String[1], Any]', :details
    optional_param 'String[1]', :issue_code
  end

  # Fail a plan, generating an exception from an existing Error object.
  # @param error An error object.
  # @return Raises an exception.
  # @example Raise an exception
  #   fail_plan(Error('We goofed up', 'task-unexpected-result', { 'result' => 'null' }))
  dispatch :from_error do
    param 'Error', :error
  end

  def from_args(msg, kind = nil, details = nil, issue_code = nil)
    unless Puppet[:tasks]
      raise Puppet::ParseErrorWithIssue
        .from_issue_and_stack(Bolt::PAL::Issues::PLAN_OPERATION_NOT_SUPPORTED_WHEN_COMPILING, action: 'fail_plan')
    end

    executor = Puppet.lookup(:bolt_executor) { nil }
    executor&.report_function_call('fail_plan')

    raise Bolt::PlanFailure.new(msg, kind || 'bolt/plan-failure', details, issue_code)
  end

  def from_error(err)
    from_args(err.message, err.kind, err.details, err.issue_code)
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
bolt-1.19.0 bolt-modules/boltlib/lib/puppet/functions/fail_plan.rb
bolt-1.18.0 bolt-modules/boltlib/lib/puppet/functions/fail_plan.rb
bolt-1.17.0 bolt-modules/boltlib/lib/puppet/functions/fail_plan.rb
bolt-1.16.0 bolt-modules/boltlib/lib/puppet/functions/fail_plan.rb
bolt-1.15.0 bolt-modules/boltlib/lib/puppet/functions/fail_plan.rb
bolt-1.14.0 bolt-modules/boltlib/lib/puppet/functions/fail_plan.rb
bolt-1.13.1 bolt-modules/boltlib/lib/puppet/functions/fail_plan.rb
bolt-1.13.0 bolt-modules/boltlib/lib/puppet/functions/fail_plan.rb