Class: ChefApply::Errors::CCRFailureMapper

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

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.



24
25
26
27
# File 'lib/chef_apply/errors/ccr_failure_mapper.rb', line 24

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

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



22
23
24
# File 'lib/chef_apply/errors/ccr_failure_mapper.rb', line 22

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.



47
48
49
50
51
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
# File 'lib/chef_apply/errors/ccr_failure_mapper.rb', line 47

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



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chef_apply/errors/ccr_failure_mapper.rb', line 29

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

  end
end