From e36735cf61f711bf38739b5df114366a7378975c Mon Sep 17 00:00:00 2001 From: John Keiser Date: Mon, 21 Apr 2014 13:19:49 -0700 Subject: Raise better exceptions in local mode (Pass related pedant tests) --- lib/chef/chef_fs/chef_fs_data_store.rb | 55 ++++++++++++++++++----- lib/chef/chef_fs/file_system/file_system_entry.rb | 11 ++++- 2 files changed, 53 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/chef/chef_fs/chef_fs_data_store.rb b/lib/chef/chef_fs/chef_fs_data_store.rb index 1fedc98380..cba21c4bcd 100644 --- a/lib/chef/chef_fs/chef_fs_data_store.rb +++ b/lib/chef/chef_fs/chef_fs_data_store.rb @@ -43,7 +43,11 @@ class Chef @memory_store.create_dir(path, name, *options) else with_dir(path) do |parent| - parent.create_child(chef_fs_filename(path + [name]), nil) + begin + parent.create_child(chef_fs_filename(path + [name]), nil) + rescue Chef::ChefFS::FileSystem::AlreadyExistsError => e + raise ChefZero::DataStore::DataAlreadyExistsError.new(to_zero_path(e.entry), e) + end end end end @@ -61,7 +65,11 @@ class Chef end with_dir(path) do |parent| - parent.create_child(chef_fs_filename(path + [name]), data) + begin + parent.create_child(chef_fs_filename(path + [name]), data) + rescue Chef::ChefFS::FileSystem::AlreadyExistsError => e + raise ChefZero::DataStore::DataAlreadyExistsError.new(to_zero_path(e.entry), e) + end end end end @@ -82,7 +90,13 @@ class Chef with_entry(path) do |entry| if path[0] == 'cookbooks' && path.length == 3 # get /cookbooks/NAME/version - result = entry.chef_object.to_hash + result = nil + begin + result = entry.chef_object.to_hash + rescue Chef::ChefFS::FileSystem::NotFoundError => e + raise ChefZero::DataStore::DataNotFoundError.new(to_zero_path(e.entry), e) + end + result.each_pair do |key, value| if value.is_a?(Array) value.each do |file| @@ -102,7 +116,11 @@ class Chef JSON.pretty_generate(result) else - entry.read + begin + entry.read + rescue Chef::ChefFS::FileSystem::NotFoundError => e + raise ChefZero::DataStore::DataNotFoundError.new(to_zero_path(e.entry), e) + end end end end @@ -121,7 +139,12 @@ class Chef write_cookbook(path, data, *options) else with_dir(path[0..-2]) do |parent| - parent.create_child(chef_fs_filename(path), data) + child = parent.child(chef_fs_filename(path)) + if child.exists? + child.write(data) + else + parent.create_child(chef_fs_filename(path), data) + end end end end @@ -132,10 +155,14 @@ class Chef @memory_store.delete(path) else with_entry(path) do |entry| - if path[0] == 'cookbooks' && path.length >= 3 - entry.delete(true) - else - entry.delete(false) + begin + if path[0] == 'cookbooks' && path.length >= 3 + entry.delete(true) + else + entry.delete(false) + end + rescue Chef::ChefFS::FileSystem::NotFoundError => e + raise ChefZero::DataStore::DataNotFoundError.new(to_zero_path(e.entry), e) end end end @@ -146,7 +173,11 @@ class Chef @memory_store.delete_dir(path, *options) else with_entry(path) do |entry| - entry.delete(options.include?(:recursive)) + begin + entry.delete(options.include?(:recursive)) + rescue Chef::ChefFS::FileSystem::NotFoundError => e + raise ChefZero::DataStore::DataNotFoundError.new(to_zero_path(e.entry), e) + end end end end @@ -186,12 +217,12 @@ class Chef with_entry(path) do |entry| begin entry.children.map { |c| zero_filename(c) }.sort - rescue Chef::ChefFS::FileSystem::NotFoundError + rescue Chef::ChefFS::FileSystem::NotFoundError => e # /cookbooks, /data, etc. never return 404 if path_always_exists?(path) [] else - raise + raise ChefZero::DataStore::DataNotFoundError.new(to_zero_path(e.entry), e) end end end diff --git a/lib/chef/chef_fs/file_system/file_system_entry.rb b/lib/chef/chef_fs/file_system/file_system_entry.rb index 46d4eb5538..b27130e1f0 100644 --- a/lib/chef/chef_fs/file_system/file_system_entry.rb +++ b/lib/chef/chef_fs/file_system/file_system_entry.rb @@ -18,8 +18,9 @@ require 'chef/chef_fs/file_system/base_fs_dir' require 'chef/chef_fs/file_system/rest_list_dir' -require 'chef/chef_fs/file_system/not_found_error' +require 'chef/chef_fs/file_system/already_exists_error' require 'chef/chef_fs/file_system/must_delete_recursively_error' +require 'chef/chef_fs/file_system/not_found_error' require 'chef/chef_fs/path_utils' require 'fileutils' @@ -48,12 +49,16 @@ class Chef def create_child(child_name, file_contents=nil) child = make_child(child_name) + if child.exists? + raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, child) + end if file_contents child.write(file_contents) else begin Dir.mkdir(child.file_path) rescue Errno::EEXIST + raise Chef::ChefFS::FileSystem::AlreadyExistsError.new(:create_child, child) end end @children = nil @@ -75,6 +80,10 @@ class Chef end end + def exists? + File.exists?(file_path) + end + def read begin File.open(file_path, "rb") {|f| f.read} -- cgit v1.2.1