summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortylercloke <tylercloke@gmail.com>2015-05-01 12:57:07 -0700
committertylercloke <tylercloke@gmail.com>2015-05-04 13:18:30 -0700
commit2160fb4f917fb9f843c2bbd11093cbb1c2873482 (patch)
tree7e13ded35494df72ca54897bf4fc5b3d7b69c240
parentddf2135f147eca68490829dd96c65690c27f8d04 (diff)
downloadchef-2160fb4f917fb9f843c2bbd11093cbb1c2873482.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.