Sha256: 27233acafa4ec25a16d548b12217e3ecbbc70031ae880aff53ee13874d1439fd

Contents?: true

Size: 1.91 KB

Versions: 3

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

module Bolt
  class Error < RuntimeError
    attr_reader :kind, :details, :issue_code, :error_code

    def initialize(msg, kind, details = nil, issue_code = nil)
      super(msg)
      @kind = kind
      @issue_code = issue_code
      @details = details || {}
      @error_code ||= 1
    end

    def msg
      message
    end

    def to_h
      h = { 'kind' =>  kind,
            'msg' => message,
            'details' => details }
      h['issue_code'] = issue_code if issue_code
      h
    end

    def to_json(opts = nil)
      to_h.to_json(opts)
    end

    def to_puppet_error
      Puppet::DataTypes::Error.from_asserted_hash(to_h)
    end

    def self.unknown_task(task)
      "Could not find a task named \"#{task}\". For a list of available tasks, run \"bolt task show\""
    end

    def self.unknown_plan(plan)
      "Could not find a plan named \"#{plan}\". For a list of available plans, run \"bolt plan show\""
    end
  end

  class RunFailure < Error
    attr_reader :result_set

    def initialize(result_set, action, object)
      details = {
        'action' => action,
        'object' =>  object,
        'result_set' => result_set
      }
      message = "Plan aborted: #{action} '#{object}' failed on #{result_set.error_set.length} nodes"
      super(message, 'bolt/run-failure', details)
      @result_set = result_set
      @error_code = 2
    end
  end

  class PlanFailure < Error
    def initialize(*args)
      super(*args)
      @error_code = 2
    end
  end

  class PuppetError < Error
    def self.from_error(err)
      new(err.msg, err.kind, err.details, err.issue_code)
    end
  end

  class InvalidPlanResult < Error
    def initialize(plan_name, result_str)
      super("Plan #{plan_name} returned an invalid result: #{result_str}",
            'bolt/invalid-plan-result',
            { 'plan_name' => plan_name,
              'result_string' => result_str })
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bolt-0.20.2 lib/bolt/error.rb
bolt-0.20.0 lib/bolt/error.rb
bolt-0.19.1 lib/bolt/error.rb