Class: ChefApply::Action::ConvergeTarget::CCRFailureMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/chef_apply/action/converge_target/ccr_failure_mapper.rb

Overview

This converts chef client run failures to human-friendly exceptions with detail and remediation steps based on the failure type.

Defined Under Namespace

Classes: RemoteChefClientRunFailed, RemoteChefClientRunFailedUnknownReason, RemoteChefRunFailedToResolveError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception, params) ⇒ CCRFailureMapper

Returns a new instance of CCRFailureMapper.



29
30
31
32
# File 'lib/chef_apply/action/converge_target/ccr_failure_mapper.rb', line 29

def initialize(exception, params)
  @params = params
  @cause_line = exception
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



27
28
29
# File 'lib/chef_apply/action/converge_target/ccr_failure_mapper.rb', line 27

def params
  @params
end

Instance Method Details

#exception_args_from_causeObject

Ideally we will write a custom handler to package up data we care about and present it more directly docs.chef.io/handlers.html For now, we’ll just match the most common failures based on their messages.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chef_apply/action/converge_target/ccr_failure_mapper.rb', line 52

def exception_args_from_cause
  # Ordering is important below.  Some earlier tests are more detailed
  # cases of things that will match more general tests further down.
  case @cause_line
  when /.*had an error:(.*:)\s+(.*$)/
    # Some invalid property value cases, among others.
    ["CHEFCCR002", $2]
  when /.*Chef::Exceptions::ValidationFailed:\s+Option action must be equal to one of:\s+(.*)!\s+You passed :(.*)\./
    # Invalid action - specialization of invalid property value, below
    ["CHEFCCR003", $2, $1]
  when /.*Chef::Exceptions::ValidationFailed:\s+(.*)/
    # Invalid resource property value
    ["CHEFCCR004", $1]
  when /.*NameError: undefined local variable or method `(.+)' for cookbook.+/
    # Invalid resource type in most cases
    ["CHEFCCR005", $1]
  when /.*NoMethodError: undefined method `(.+)' for cookbook.+/
    # Invalid resource type in most cases
    ["CHEFCCR005", $1]
  when /.*undefined method `(.*)' for (.+)/
    # Unknown resource property
    ["CHEFCCR006", $1, $2]

    # Below would catch the general form of most errors, but the
    # message itself in those lines is not generally aligned
    # with the UX we want to provide.
    # when /.*Exception|Error.*:\s+(.*)/
  else
    nil
  end
end

#raise_mapped_exception!Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/chef_apply/action/converge_target/ccr_failure_mapper.rb', line 34

def raise_mapped_exception!
  if @cause_line.nil?
    raise RemoteChefRunFailedToResolveError.new(params[:failed_report_path])
  else
    errid, *args = exception_args_from_cause
    if errid.nil?
      raise RemoteChefClientRunFailedUnknownReason.new
    else
      raise RemoteChefClientRunFailed.new(errid, *args)
    end

  end
end