chef / chef

Chef Infra, a powerful automation platform that transforms infrastructure into code automating how infrastructure is configured, deployed and managed across any environment, at any scale
http://www.chef.io/chef/
Apache License 2.0
7.56k stars 2.51k forks source link

data_bag('bag') and data_bag_item('bag', 'item') raise unhelpful 404 #7094

Open atheiman opened 6 years ago

atheiman commented 6 years ago

can the data_bag() and data_bag_item() DSL methods provide more helpful error messages than

Net::HTTPServerException
------------------------
404 "Not Found "

Ideally the error message could at least list the URL which would have the data bag name and the item name printed in the URL (/organizations/NAME/data/NAME/ITEM).

Even better would be an error message that showed the bag name and item name more clearly than the URL (Data bag not found: '#{bag_name}' or Data bag item not found '#{item_name}' in data bag '#{bag_name}')

atheiman commented 6 years ago

Well there is an error log message, but I still would rather the error was in the exception rather than a separate log above it. For some chefspec tests the log was hidden from me b/c of my log_level configured to :fatal, dropping the log_level to :error shows the message as expected.

Could the 404 exception be inherited into a DataBagException or DataBagItemException? That should be backwards compatible to work with existing recipes like:

begin
  # get the item
  item = data_bag_item('bag', 'item')
rescue Net::HTTPServerException
  # item doesnt exist, create the item
  item = Chef::DataBagItem.new
  item.data_bag('bag')
  item.raw_data('id' => 'item', 'a' => 'A', 'b' => 'B')
  item.save
end

https://github.com/chef/chef/blob/2cd379b12eac02246ecc12b5e3cbb51069c9cedc/lib/chef/dsl/data_query.rb#L53-L55