summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaire McQuin <claire@getchef.com>2014-05-14 10:06:17 -0700
committerClaire McQuin <claire@getchef.com>2014-05-14 10:06:17 -0700
commitd73d0cb8166c2839944cdd9a048f5b7474f62ba7 (patch)
tree7513ecd83db08bc01ee5eebe19af623ff377d3f0
parent7db7152f3a86ea568b09db458165715bd791c608 (diff)
downloadchef-d73d0cb8166c2839944cdd9a048f5b7474f62ba7.tar.gz
Refactor rescue behavior.
-rw-r--r--lib/chef/formatters/error_inspectors/api_error_formatting.rb32
-rw-r--r--lib/chef/mixin/create_path.rb35
2 files changed, 38 insertions, 29 deletions
diff --git a/lib/chef/formatters/error_inspectors/api_error_formatting.rb b/lib/chef/formatters/error_inspectors/api_error_formatting.rb
index dca2835f23..652d478b40 100644
--- a/lib/chef/formatters/error_inspectors/api_error_formatting.rb
+++ b/lib/chef/formatters/error_inspectors/api_error_formatting.rb
@@ -88,20 +88,7 @@ E
def format_rest_error
Array(Chef::JSONCompat.from_json(exception.response.body)["error"]).join('; ')
rescue Exception
- # When we get 504 from the server, sometimes the response body is non-readable.
- #
- # Stack trace:
- #
- # NoMethodError: undefined method `closed?' for nil:NilClass
- # .../lib/ruby/1.9.1/net/http.rb:2789:in `stream_check'
- # .../lib/ruby/1.9.1/net/http.rb:2709:in `read_body'
- # .../lib/ruby/1.9.1/net/http.rb:2736:in `body'
- # .../lib/chef/formatters/error_inspectors/api_error_formatting.rb:91:in `rescue in format_rest_error'
- begin
- exception.response.body
- rescue Exception
- "Cannot fetch the contents of the response."
- end
+ safe_format_rest_error
end
def username
@@ -120,6 +107,23 @@ E
exception.response.body =~ /synchronize the clock/i
end
+ def safe_format_rest_error
+ # When we get 504 from the server, sometimes the response body is non-readable.
+ #
+ # Stack trace:
+ #
+ # NoMethodError: undefined method `closed?' for nil:NilClass
+ # .../lib/ruby/1.9.1/net/http.rb:2789:in `stream_check'
+ # .../lib/ruby/1.9.1/net/http.rb:2709:in `read_body'
+ # .../lib/ruby/1.9.1/net/http.rb:2736:in `body'
+ # .../lib/chef/formatters/error_inspectors/api_error_formatting.rb:91:in `rescue in format_rest_error'
+ begin
+ exception.response.body
+ rescue Exception
+ "Cannot fetch the contents of the response."
+ end
+ end
+
end
end
end
diff --git a/lib/chef/mixin/create_path.rb b/lib/chef/mixin/create_path.rb
index ef8f381635..ff20a65cb1 100644
--- a/lib/chef/mixin/create_path.rb
+++ b/lib/chef/mixin/create_path.rb
@@ -44,25 +44,30 @@ class Chef
file_path.each_index do |i|
create_path = File.join(file_path[0, i + 1])
- unless File.directory?(create_path)
- begin
- # In multithreaded environments, the following interleaving raises
- # an error here:
- #
- # thread1 thread2
- # File.directory?(create_path) <- false
- # File.directory?(create_path) <- false
- # Dir.mkdir(create_path)
- # Dir.mkdir(create_path) <- raises Errno::EEXIST
- Chef::Log.debug("Creating directory #{create_path}")
- Dir.mkdir(create_path)
- rescue Errno::EEXIST
- end
- end
+ create_dir(create_path) unless File.directory?(create_path)
end
+
File.expand_path(File.join(file_path))
end
+ private
+
+ def create_dir(path)
+ begin
+ # In multithreaded environments, the following interleaving raises
+ # an error here:
+ #
+ # thread1 thread2
+ # File.directory?(create_path) <- false
+ # File.directory?(create_path) <- false
+ # Dir.mkdir(create_path)
+ # Dir.mkdir(create_path) <- raises Errno::EEXIST
+ Chef::Log.debug("Creating directory #{path}")
+ Dir.mkdir(path)
+ rescue Errno::EEXIST
+ end
+ end
+
end
end
end