summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortylercloke <tylercloke@gmail.com>2015-05-01 12:57:07 -0700
committertylercloke <tylercloke@gmail.com>2015-05-04 11:00:34 -0700
commit7b142bced6090cdf70d1b56bd0d6d19ba6bbec89 (patch)
tree2e160d2740d887c54a6d8f79da9830bd283e4e0a
parenta8d997e39539a6f1856d2e50b19a2a6d075c08f2 (diff)
downloadchef-7b142bced6090cdf70d1b56bd0d6d19ba6bbec89.tar.gz
Nice error formatting for unsupported API version in Knife.
-rw-r--r--lib/chef/knife.rb8
-rw-r--r--spec/unit/knife_spec.rb10
2 files changed, 18 insertions, 0 deletions
diff --git a/lib/chef/knife.rb b/lib/chef/knife.rb
index 2e0694aebc..4c59f831de 100644
--- a/lib/chef/knife.rb
+++ b/lib/chef/knife.rb
@@ -27,6 +27,7 @@ require 'chef/knife/core/subcommand_loader'
require 'chef/knife/core/ui'
require 'chef/local_mode'
require 'chef/rest'
+require 'chef/http/authenticator'
require 'pp'
class Chef
@@ -483,6 +484,13 @@ class Chef
when Net::HTTPServiceUnavailable
ui.error "Service temporarily unavailable"
ui.info "Response: #{format_rest_error(response)}"
+ when Net::HTTPNotAcceptable
+ min_version = Chef::JSONCompat.from_json(response.body)["min_version"]
+ max_version = Chef::JSONCompat.from_json(response.body)["max_version"]
+ ui.error "The version of Chef that Knife is using is not supported by the Chef server you sent this request to"
+ ui.info "This version of Chef requires a server API version of #{Chef::HTTP::Authenticator::SERVER_API_VERSION}"
+ ui.info "The Chef server you sent the request to supports a min API verson of #{min_version} and a max API version of #{max_version}"
+ ui.info "Please either update your Chef client or server to be a compatible set"
else
ui.error response.message
ui.info "Response: #{format_rest_error(response)}"
diff --git a/spec/unit/knife_spec.rb b/spec/unit/knife_spec.rb
index cb7d5e0b9e..a0afafb6b9 100644
--- a/spec/unit/knife_spec.rb
+++ b/spec/unit/knife_spec.rb
@@ -375,6 +375,16 @@ describe Chef::Knife do
expect(stderr.string).to match(%r[Response: nothing to see here])
end
+ it "formats 406s (non-supported API version error) nicely" do
+ response = Net::HTTPNotAcceptable.new("1.1", "406", "Not Acceptable")
+ response.instance_variable_set(:@read, true) # I hate you, net/http.
+ allow(response).to receive(:body).and_return(Chef::JSONCompat.to_json(:error => "sad trombone", :min_version => "0", :max_version => "1"))
+ allow(knife).to receive(:run).and_raise(Net::HTTPServerException.new("406 Not Acceptable", response))
+ knife.run_with_pretty_exceptions
+ expect(stderr.string).to match(%r[The version of Chef that Knife is using is not supported by the Chef server you sent this request to])
+ expect(stderr.string).to match(%r[This version of Chef requires a server API version of #{Chef::HTTP::Authenticator::SERVER_API_VERSION}])
+ end
+
it "formats 500s nicely" do
response = Net::HTTPInternalServerError.new("1.1", "500", "Internal Server Error")
response.instance_variable_set(:@read, true) # I hate you, net/http.