summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2020-08-10 10:14:21 -0700
committerGitHub <noreply@github.com>2020-08-10 10:14:21 -0700
commit0c9744cdb82b13c890f207e88b0b74e611527023 (patch)
tree6348943edcf38b8e81d231e6b18aa05ec0449667
parent5c8383fedd13b07f13d64a58f7cc78664a235ced (diff)
parent45dcd20269c5394f52f0314cc8789cc84f747510 (diff)
downloadchef-0c9744cdb82b13c890f207e88b0b74e611527023.tar.gz
Merge pull request #10237 from chef/VSingh/client-run-error-per-resource
client-run per resource error detail
-rw-r--r--lib/chef/action_collection.rb4
-rw-r--r--lib/chef/data_collector/run_end_message.rb12
-rw-r--r--spec/unit/data_collector_spec.rb22
3 files changed, 37 insertions, 1 deletions
diff --git a/lib/chef/action_collection.rb b/lib/chef/action_collection.rb
index efe03fff5f..1ac47630a9 100644
--- a/lib/chef/action_collection.rb
+++ b/lib/chef/action_collection.rb
@@ -44,6 +44,9 @@ class Chef
# @return [Exception] The exception that was thrown
attr_accessor :exception
+ # @return [Hash] JSON-formatted error description from the Chef::Formatters::ErrorMapper
+ attr_accessor :error_description
+
# @return [Numeric] The elapsed time in seconds with machine precision
attr_accessor :elapsed_time
@@ -223,6 +226,7 @@ class Chef
current_record.status = :failed
current_record.exception = exception
+ current_record.error_description = Formatters::ErrorMapper.resource_failed(new_resource, action, exception).for_json
end
# Hook called after an action is completed. This is always called, even if the action fails.
diff --git a/lib/chef/data_collector/run_end_message.rb b/lib/chef/data_collector/run_end_message.rb
index 4bb4fe2852..6f9f90b323 100644
--- a/lib/chef/data_collector/run_end_message.rb
+++ b/lib/chef/data_collector/run_end_message.rb
@@ -133,7 +133,17 @@ class Chef
end
hash["conditional"] = action_record.conditional.to_text if action_record.status == :skipped
- hash["error_message"] = action_record.exception.message unless action_record.exception.nil?
+
+ unless action_record.exception.nil?
+ hash["error_message"] = action_record.exception.message
+
+ hash["error"] = {
+ "class" => action_record.exception.class,
+ "message" => action_record.exception.message,
+ "backtrace" => action_record.exception.backtrace,
+ "description" => action_record.error_description,
+ }
+ end
hash
end
diff --git a/spec/unit/data_collector_spec.rb b/spec/unit/data_collector_spec.rb
index 79d5ed7d5f..d3a14716cc 100644
--- a/spec/unit/data_collector_spec.rb
+++ b/spec/unit/data_collector_spec.rb
@@ -39,6 +39,7 @@ describe Chef::DataCollector do
let(:new_resource) do
new_resource = Chef::Resource::File.new("/tmp/a-file.txt")
new_resource.checksum nil
+ new_resource.path
new_resource
end
@@ -780,6 +781,13 @@ describe Chef::DataCollector do
let(:resource_record) do
rec = resource_record_for(new_resource, current_resource, nil, :create, "failed", "1234")
rec["error_message"] = "imperial to metric conversion error"
+ rec["error"] = {
+ "class" => exception.class,
+ "message" => exception.message,
+ "backtrace" => exception.backtrace,
+ "description" => error_description,
+ }
+
[ rec ]
end
let(:status) { "failure" }
@@ -808,6 +816,13 @@ describe Chef::DataCollector do
rec = resource_record_for(new_resource, nil, nil, :create, "failed", "1234")
rec["before"] = {}
rec["error_message"] = "imperial to metric conversion error"
+ rec["error"] = {
+ "class" => exception.class,
+ "message" => exception.message,
+ "backtrace" => exception.backtrace,
+ "description" => error_description,
+ }
+
[ rec ]
end
let(:status) { "failure" }
@@ -842,6 +857,13 @@ describe Chef::DataCollector do
let(:resource_record) do
rec1 = resource_record_for(new_resource, current_resource, nil, :create, "failed", "1234")
rec1["error_message"] = "imperial to metric conversion error"
+ rec1["error"] = {
+ "class" => exception.class,
+ "message" => exception.message,
+ "backtrace" => exception.backtrace,
+ "description" => error_description,
+ }
+
rec2 = resource_record_for(unprocessed_resource, nil, nil, :nothing, "unprocessed", "")
[ rec1, rec2 ]
end