From f245630612e84d9b575a46b90d9de552b980c16c Mon Sep 17 00:00:00 2001 From: John Keiser Date: Mon, 20 May 2013 23:28:06 -0700 Subject: Catch timeout exception in http requests --- lib/chef/chef_fs/command_line.rb | 19 ++----------------- lib/chef/chef_fs/file_system/acl_entry.rb | 2 ++ lib/chef/chef_fs/file_system/cookbook_dir.rb | 24 ++++++++++++++++-------- lib/chef/chef_fs/file_system/cookbook_file.rb | 2 ++ lib/chef/chef_fs/file_system/cookbooks_dir.rb | 4 +++- lib/chef/chef_fs/file_system/data_bag_dir.rb | 10 +++++++--- lib/chef/chef_fs/file_system/data_bags_dir.rb | 18 +++++++++++------- lib/chef/chef_fs/file_system/nodes_dir.rb | 2 ++ lib/chef/chef_fs/file_system/rest_list_dir.rb | 4 ++++ lib/chef/chef_fs/file_system/rest_list_entry.rb | 18 +++++++++++------- lib/chef/knife/raw.rb | 3 +++ 11 files changed, 63 insertions(+), 43 deletions(-) diff --git a/lib/chef/chef_fs/command_line.rb b/lib/chef/chef_fs/command_line.rb index dcb7569d4e..dd5e62b755 100644 --- a/lib/chef/chef_fs/command_line.rb +++ b/lib/chef/chef_fs/command_line.rb @@ -91,6 +91,7 @@ class Chef else yield "Only in #{format_path.call(new_entry.parent)}: #{new_entry.name}\n" end + when :modified next if diff_filter && diff_filter !~ /M/ if output_mode == :name_only @@ -102,6 +103,7 @@ class Chef result << diff_text(old_path, new_path, old_value, new_value) yield result end + when :both_nonexistent when :added_cannot_upload when :deleted_cannot_download @@ -233,23 +235,6 @@ class Chef return [ [ :error, old_entry, new_entry, nil, nil, e ] ] end - class Differ - def initialize(old_entry, new_entry, recurse_depth, get_content) - @old_entry = old_entry - @new_entry = new_entry - @recurse_depth = recurse_depth - @get_content = get_content - end - - attr_reader :old_entry - attr_reader :new_entry - attr_reader :recurse_depth - attr_reader :get_content - - def each - end - end - private def self.sort_keys(json_object) diff --git a/lib/chef/chef_fs/file_system/acl_entry.rb b/lib/chef/chef_fs/file_system/acl_entry.rb index 9ecd5dd364..0be9076038 100644 --- a/lib/chef/chef_fs/file_system/acl_entry.rb +++ b/lib/chef/chef_fs/file_system/acl_entry.rb @@ -41,6 +41,8 @@ class Chef PERMISSIONS.each do |permission| begin rest.put_rest("#{api_path}/#{permission}", { permission => acls[permission] }) + rescue Timeout::Error => e + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:write, self, e), "Timeout writing: #{e}" rescue Net::HTTPServerException => e if e.response.code == "404" raise Chef::ChefFS::FileSystem::NotFoundError.new(self, e) diff --git a/lib/chef/chef_fs/file_system/cookbook_dir.rb b/lib/chef/chef_fs/file_system/cookbook_dir.rb index debce979f8..4e76d81c33 100644 --- a/lib/chef/chef_fs/file_system/cookbook_dir.rb +++ b/lib/chef/chef_fs/file_system/cookbook_dir.rb @@ -126,9 +126,13 @@ class Chef if recurse begin rest.delete_rest(api_path) + rescue Timeout::Error => e + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:delete, self, e), "Timeout deleting: #{e}" rescue Net::HTTPServerException if $!.response.code == "404" raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!) + else + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:delete, self, e), "HTTP error deleting: #{e}" end end else @@ -190,22 +194,26 @@ class Chef ensure Chef::Config[:http_retry_count] = old_retry_count end - rescue Net::HTTPServerException - if $!.response.code == "404" - @could_not_get_chef_object = $! + + rescue Timeout::Error => e + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:read, self, e), "Timeout reading: #{e}" + + rescue Net::HTTPServerException => e + if e.response.code == "404" + @could_not_get_chef_object = e raise Chef::ChefFS::FileSystem::NotFoundError.new(self, @could_not_get_chef_object) else - raise + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:read, self, e), "HTTP error reading: #{e}" end # Chef bug http://tickets.opscode.com/browse/CHEF-3066 ... instead of 404 we get 500 right now. # Remove this when that bug is fixed. - rescue Net::HTTPFatalError - if $!.response.code == "500" - @could_not_get_chef_object = $! + rescue Net::HTTPFatalError => e + if e.response.code == "500" + @could_not_get_chef_object = e raise Chef::ChefFS::FileSystem::NotFoundError.new(self, @could_not_get_chef_object) else - raise + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:read, self, e), "HTTP error reading: #{e}" end end end diff --git a/lib/chef/chef_fs/file_system/cookbook_file.rb b/lib/chef/chef_fs/file_system/cookbook_file.rb index b477223e0c..e05c4aa614 100644 --- a/lib/chef/chef_fs/file_system/cookbook_file.rb +++ b/lib/chef/chef_fs/file_system/cookbook_file.rb @@ -39,6 +39,8 @@ class Chef rest.sign_on_redirect = false begin tmpfile = rest.get_rest(file[:url], true) + rescue Timeout::Error => e + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:read, self, e), "Timeout reading #{file[:url]}: #{e}" rescue Net::HTTPServerException => e raise Chef::ChefFS::FileSystem::OperationFailedError.new(:read, self, e), "#{e.message} retrieving #{file[:url]}" ensure diff --git a/lib/chef/chef_fs/file_system/cookbooks_dir.rb b/lib/chef/chef_fs/file_system/cookbooks_dir.rb index 215c9387b4..271f25571f 100644 --- a/lib/chef/chef_fs/file_system/cookbooks_dir.rb +++ b/lib/chef/chef_fs/file_system/cookbooks_dir.rb @@ -65,6 +65,8 @@ class Chef def upload_cookbook_from(other) Chef::Config[:versioned_cookbooks] ? upload_versioned_cookbook(other) : upload_unversioned_cookbook(other) + rescue Timeout::Error => e + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:write, self, e), "Timeout writing: #{e}" rescue Net::HTTPServerException => e case e.response.code when "409" @@ -72,7 +74,7 @@ class Chef Chef::Log.debug(e) raise Exceptions::CookbookFrozen else - raise + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:write, self, e), "HTTP error writing: #{e}" end end diff --git a/lib/chef/chef_fs/file_system/data_bag_dir.rb b/lib/chef/chef_fs/file_system/data_bag_dir.rb index ff91137841..3814b94fac 100644 --- a/lib/chef/chef_fs/file_system/data_bag_dir.rb +++ b/lib/chef/chef_fs/file_system/data_bag_dir.rb @@ -53,9 +53,13 @@ class Chef end begin rest.delete_rest(api_path) - rescue Net::HTTPServerException - if $!.response.code == "404" - raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!) + rescue Timeout::Error => e + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:delete, self, e), "Timeout deleting: #{e}" + rescue Net::HTTPServerException => e + if e.response.code == "404" + raise Chef::ChefFS::FileSystem::NotFoundError.new(self, e) + else + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:delete, self, e), "HTTP error deleting: #{e}" end end end diff --git a/lib/chef/chef_fs/file_system/data_bags_dir.rb b/lib/chef/chef_fs/file_system/data_bags_dir.rb index 3304edf2cd..fb6b79d912 100644 --- a/lib/chef/chef_fs/file_system/data_bags_dir.rb +++ b/lib/chef/chef_fs/file_system/data_bags_dir.rb @@ -37,11 +37,13 @@ class Chef @children ||= Chef::ChefFS::RawRequest.raw_json(rest, api_path).keys.sort.map do |entry| DataBagDir.new(entry, self, true) end - rescue Net::HTTPServerException - if $!.response.code == "404" - raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!) + rescue Timeout::Error => e + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:children, self, e), "Timeout getting children: #{e}" + rescue Net::HTTPServerException => e + if e.response.code == "404" + raise Chef::ChefFS::FileSystem::NotFoundError.new(self, e) else - raise + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:children, self, e), "HTTP error getting children: #{e}" end end end @@ -53,9 +55,11 @@ class Chef def create_child(name, file_contents) begin rest.post_rest(api_path, { 'name' => name }) - rescue Net::HTTPServerException - if $!.response.code != "409" - raise + rescue Timeout::Error => e + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:create_child, self, e), "Timeout creating child '#{name}': #{e}" + rescue Net::HTTPServerException => e + if e.response.code != "409" + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:create_child, self, e), "HTTP error creating child '#{name}': #{e}" end end DataBagDir.new(name, self, true) diff --git a/lib/chef/chef_fs/file_system/nodes_dir.rb b/lib/chef/chef_fs/file_system/nodes_dir.rb index 607a0f26ca..82683e81ac 100644 --- a/lib/chef/chef_fs/file_system/nodes_dir.rb +++ b/lib/chef/chef_fs/file_system/nodes_dir.rb @@ -35,6 +35,8 @@ class Chef @children ||= Chef::ChefFS::RawRequest.raw_json(rest, env_api_path).keys.sort.map do |key| _make_child_entry("#{key}.json", true) end + rescue Timeout::Error => e + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:children, self, e), "Timeout retrieving children: #{e}" rescue Net::HTTPServerException => e if $!.response.code == "404" raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!) diff --git a/lib/chef/chef_fs/file_system/rest_list_dir.rb b/lib/chef/chef_fs/file_system/rest_list_dir.rb index bccdc8b57b..1acac8ee91 100644 --- a/lib/chef/chef_fs/file_system/rest_list_dir.rb +++ b/lib/chef/chef_fs/file_system/rest_list_dir.rb @@ -48,6 +48,8 @@ class Chef @children ||= Chef::ChefFS::RawRequest.raw_json(rest, api_path).keys.sort.map do |key| _make_child_entry("#{key}.json", true) end + rescue Timeout::Error => e + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:children, self, e), "Timeout retrieving children: #{e}" rescue Net::HTTPServerException => e if $!.response.code == "404" raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!) @@ -75,6 +77,8 @@ class Chef begin rest.post_rest(api_path, object) + rescue Timeout::Error => e + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:create_child, self, e), "Timeout creating '#{name}': #{e}" rescue Net::HTTPServerException => e if e.response.code == "404" raise Chef::ChefFS::FileSystem::NotFoundError.new(self, e) diff --git a/lib/chef/chef_fs/file_system/rest_list_entry.rb b/lib/chef/chef_fs/file_system/rest_list_entry.rb index aed2ce6403..6e6ad12438 100644 --- a/lib/chef/chef_fs/file_system/rest_list_entry.rb +++ b/lib/chef/chef_fs/file_system/rest_list_entry.rb @@ -69,11 +69,13 @@ class Chef def delete(recurse) begin rest.delete_rest(api_path) - rescue Net::HTTPServerException - if $!.response.code == "404" - raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!) + rescue Timeout::Error => e + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:delete, self, e), "Timeout deleting: #{e}" + rescue Net::HTTPServerException => e + if e.response.code == "404" + raise Chef::ChefFS::FileSystem::NotFoundError.new(self, e) else - raise + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:delete, self, e), "Timeout deleting: #{e}" end end end @@ -85,9 +87,11 @@ class Chef def _read_hash begin json = Chef::ChefFS::RawRequest.raw_request(rest, api_path) + rescue Timeout::Error => e + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:read, self, e), "Timeout reading: #{e}" rescue Net::HTTPServerException => e if $!.response.code == "404" - raise Chef::ChefFS::FileSystem::NotFoundError.new(self, $!) + raise Chef::ChefFS::FileSystem::NotFoundError.new(self, e) else raise Chef::ChefFS::FileSystem::OperationFailedError.new(:read, self, e), "HTTP error reading: #{e}" end @@ -126,7 +130,6 @@ class Chef value = minimize_value(value) value_json = Chef::JSONCompat.to_json_pretty(value) begin - #other_value = Chef::JSONCompat.from_json(other_value_json, :create_additions => false) other_value = JSON.parse(other_value_json, :create_additions => false) rescue JSON::ParserError => e Chef::Log.warn("Parse error reading #{other.path_for_printing} as JSON: #{e}") @@ -144,7 +147,6 @@ class Chef def write(file_contents) begin - #object = Chef::JSONCompat.from_json(file_contents).to_hash object = JSON.parse(file_contents, :create_additions => false) rescue JSON::ParserError => e raise Chef::ChefFS::FileSystem::OperationFailedError.new(:write, self, e), "Parse error reading JSON: #{e}" @@ -159,6 +161,8 @@ class Chef begin rest.put_rest(api_path, object) + rescue Timeout::Error => e + raise Chef::ChefFS::FileSystem::OperationFailedError.new(:write, self, e), "Timeout writing: #{e}" rescue Net::HTTPServerException => e if e.response.code == "404" raise Chef::ChefFS::FileSystem::NotFoundError.new(self, e) diff --git a/lib/chef/knife/raw.rb b/lib/chef/knife/raw.rb index 52cc6c54e3..3aa4fd8049 100644 --- a/lib/chef/knife/raw.rb +++ b/lib/chef/knife/raw.rb @@ -43,6 +43,9 @@ class Chef chef_rest = Chef::REST.new(Chef::Config[:chef_server_url]) begin output Chef::ChefFS::RawRequest.api_request(chef_rest, config[:method].to_sym, chef_rest.create_url(name_args[0]), {}, data) + rescue Timeout::Error => e + ui.error "Server timeout" + exit 1 rescue Net::HTTPServerException => e ui.error "Server responded with error #{e.response.code} \"#{e.response.message}\"" ui.error "Error Body: #{e.response.body}" if e.response.body && e.response.body != '' -- cgit v1.2.1