From ba8264ca9ecf8192abe7c6eb41c310f09e9e049d Mon Sep 17 00:00:00 2001 From: danielsdeleo Date: Tue, 19 May 2015 15:56:25 -0700 Subject: Show cookbook trace when available regardless of exception type --- .../error_inspectors/compile_error_inspector.rb | 30 +++++++++++++++------- .../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 -- cgit v1.2.1