diff options
author | danielsdeleo <dan@getchef.com> | 2015-05-19 15:56:25 -0700 |
---|---|---|
committer | danielsdeleo <dan@getchef.com> | 2015-05-20 10:42:28 -0700 |
commit | 6606220b11fb6de247cf1335f681ba8080c79c79 (patch) | |
tree | d654f6c7ef89acf7ed477cb1c8c687abd05b00f4 | |
parent | a0fad76df616ccc662f3eb660f8d74a96f2a72a4 (diff) | |
download | chef-6606220b11fb6de247cf1335f681ba8080c79c79.tar.gz |
Show cookbook trace when available regardless of exception type
-rw-r--r-- | lib/chef/formatters/error_inspectors/compile_error_inspector.rb | 30 | ||||
-rw-r--r-- | spec/unit/formatters/error_inspectors/compile_error_inspector_spec.rb | 16 |
2 files changed, 37 insertions, 9 deletions
diff --git a/lib/chef/formatters/error_inspectors/compile_error_inspector.rb b/lib/chef/formatters/error_inspectors/compile_error_inspector.rb index 93328adbe3..d64d5e7b01 100644 --- a/lib/chef/formatters/error_inspectors/compile_error_inspector.rb +++ b/lib/chef/formatters/error_inspectors/compile_error_inspector.rb @@ -30,15 +30,16 @@ class Chef def initialize(path, exception) @path, @exception = path, exception + @backtrace_lines_in_cookbooks = nil + @file_lines = nil + @culprit_backtrace_entry = nil + @culprit_line = nil end def add_explanation(error_description) - case exception - when Chef::Exceptions::RecipeNotFound - error_description.section(exception.class.name, exception.message) - else - error_description.section(exception.class.name, exception.message) + error_description.section(exception.class.name, exception.message) + if found_error_in_cookbooks? traceback = filtered_bt.map {|line| " #{line}"}.join("\n") error_description.section("Cookbook Trace:", traceback) error_description.section("Relevant File Content:", context) @@ -93,10 +94,21 @@ class Chef end def filtered_bt - filters = Array(Chef::Config.cookbook_path).map {|p| /^#{Regexp.escape(p)}/ } - r = exception.backtrace.select {|line| filters.any? {|filter| line =~ filter }} - Chef::Log.debug("filtered backtrace of compile error: #{r.join(",")}") - return r.count > 0 ? r : exception.backtrace + backtrace_lines_in_cookbooks.count > 0 ? backtrace_lines_in_cookbooks : exception.backtrace + end + + def found_error_in_cookbooks? + !backtrace_lines_in_cookbooks.empty? + end + + def backtrace_lines_in_cookbooks + @backtrace_lines_in_cookbooks ||= + begin + filters = Array(Chef::Config.cookbook_path).map {|p| /^#{Regexp.escape(p)}/i } + r = exception.backtrace.select {|line| filters.any? {|filter| line =~ filter }} + Chef::Log.debug("filtered backtrace of compile error: #{r.join(",")}") + r + end end end diff --git a/spec/unit/formatters/error_inspectors/compile_error_inspector_spec.rb b/spec/unit/formatters/error_inspectors/compile_error_inspector_spec.rb index d957a97284..5f95beb259 100644 --- a/spec/unit/formatters/error_inspectors/compile_error_inspector_spec.rb +++ b/spec/unit/formatters/error_inspectors/compile_error_inspector_spec.rb @@ -96,6 +96,10 @@ describe Chef::Formatters::ErrorInspectors::CompileErrorInspector do inspector.add_explanation(description) end + it "reports the error was not located within cookbooks" do + expect(inspector.found_error_in_cookbooks?).to be(true) + end + it "finds the line number of the error from the stacktrace" do expect(inspector.culprit_line).to eq(14) end @@ -138,6 +142,10 @@ describe Chef::Formatters::ErrorInspectors::CompileErrorInspector do expect { description.display(outputter) }.to_not raise_error end + it "reports the error was not located within cookbooks" do + expect(inspector.found_error_in_cookbooks?).to be(false) + end + end end @@ -184,6 +192,10 @@ describe Chef::Formatters::ErrorInspectors::CompileErrorInspector do let(:full_path_to_failed_file) { "C:/opscode/chef#{path_to_failed_file}" } + it "reports the error was not located within cookbooks" do + expect(inspector.found_error_in_cookbooks?).to be(true) + end + it "finds the culprit recipe name" do expect(inspector.culprit_file).to eq("C:/opscode/chef/var/cache/cookbooks/foo/recipes/default.rb") end @@ -205,6 +217,10 @@ describe Chef::Formatters::ErrorInspectors::CompileErrorInspector do let(:full_path_to_failed_file) { "c:/opscode/chef#{path_to_failed_file}" } + it "reports the error was not located within cookbooks" do + expect(inspector.found_error_in_cookbooks?).to be(true) + end + it "finds the culprit recipe name from the stacktrace" do expect(inspector.culprit_file).to eq("c:/opscode/chef/var/cache/cookbooks/foo/recipes/default.rb") end |