Class: Chef::Formatters::ErrorInspectors::ResourceFailureInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/formatters/error_inspectors/resource_failure_inspector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, action, exception) ⇒ ResourceFailureInspector

Returns a new instance of ResourceFailureInspector.



30
31
32
33
34
# File 'lib/chef/formatters/error_inspectors/resource_failure_inspector.rb', line 30

def initialize(resource, action, exception)
  @resource = resource
  @action = action
  @exception = exception
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



27
28
29
# File 'lib/chef/formatters/error_inspectors/resource_failure_inspector.rb', line 27

def action
  @action
end

#exceptionObject (readonly)

Returns the value of attribute exception.



28
29
30
# File 'lib/chef/formatters/error_inspectors/resource_failure_inspector.rb', line 28

def exception
  @exception
end

#resourceObject (readonly)

Returns the value of attribute resource.



26
27
28
# File 'lib/chef/formatters/error_inspectors/resource_failure_inspector.rb', line 26

def resource
  @resource
end

Instance Method Details

#add_explanation(error_description) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chef/formatters/error_inspectors/resource_failure_inspector.rb', line 36

def add_explanation(error_description)
  error_description.section(exception.class.name, exception.message)

  unless filtered_bt.empty?
    error_description.section("Cookbook Trace: (most recent call first)", filtered_bt.join("\n"))
  end

  unless dynamic_resource?
    error_description.section("Resource Declaration:", resource.sensitive ? "suppressed sensitive resource output" : recipe_snippet)
  end

  error_description.section("Compiled Resource:", (resource.to_text).to_s)

  # Template errors get wrapped in an exception class that can show the relevant template code,
  # so add them to the error output.
  if exception.respond_to?(:source_listing)
    error_description.section("Template Context:", "#{exception.source_location}\n#{exception.source_listing}")
  end

  if ChefUtils.windows?
    require_relative "../../win32/security"

    unless Chef::ReservedNames::Win32::Security.has_admin_privileges?
      error_description.section("Missing Windows Admin Privileges", "#{ChefUtils::Dist::Infra::CLIENT} doesn't have administrator privileges. This can be a possible reason for the resource failure.")
    end
  end
end

#dynamic_resource?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/chef/formatters/error_inspectors/resource_failure_inspector.rb', line 97

def dynamic_resource?
  !resource.source_line
end

#filtered_btObject



101
102
103
104
# File 'lib/chef/formatters/error_inspectors/resource_failure_inspector.rb', line 101

def filtered_bt
  filters = Array(Chef::Config.cookbook_path).map { |p| /^#{Regexp.escape(p)}/ }
  exception.backtrace.select { |line| filters.any? { |filter| line =~ filter } }
end

#recipe_snippetObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/chef/formatters/error_inspectors/resource_failure_inspector.rb', line 64

def recipe_snippet
  return nil if dynamic_resource?

  @snippet ||= if (file = parse_source) && (line = parse_line(file))
                 return nil unless ::File.exist?(file)

                 lines = IO.readlines(file)

                 relevant_lines = ["# In #{file}\n\n"]

                 current_line = line - 1
                 current_line = 0 if current_line < 0
                 nesting = 0

                 loop do

                   # low rent parser. try to gracefully handle nested blocks in resources
                   nesting += 1 if /\s+do\s*/.match?(lines[current_line])
                   nesting -= 1 if /end\s*$/.match?(lines[current_line])

                   relevant_lines << format_line(current_line, lines[current_line])

                   break if lines[current_line + 1].nil?
                   break if current_line >= (line + 50)
                   break if nesting <= 0

                   current_line += 1
                 end
                 relevant_lines << format_line(current_line + 1, lines[current_line + 1]) if lines[current_line + 1]
                 relevant_lines.join("")
               end
end