summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaire McQuin <claire@getchef.com>2014-09-04 12:37:45 -0700
committerClaire McQuin <claire@getchef.com>2014-09-04 15:52:47 -0700
commit8022a0d89a85787a4b6550428ab26ae37b09b8c0 (patch)
treea7bcf6ff5d7e677c2c84724bc83519d19bd6c414
parent260278d883c86dfab0c9bb933bdbe91cae752a3f (diff)
downloadchef-8022a0d89a85787a4b6550428ab26ae37b09b8c0.tar.gz
Add InvalidSearchQuery exception for malformed searches.
-rw-r--r--lib/chef/exceptions.rb2
-rw-r--r--lib/chef/search/query.rb21
-rw-r--r--spec/unit/search/query_spec.rb5
3 files changed, 22 insertions, 6 deletions
diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb
index f6db5dbe56..23e223f204 100644
--- a/lib/chef/exceptions.rb
+++ b/lib/chef/exceptions.rb
@@ -347,5 +347,7 @@ class Chef
class EncodeError < RuntimeError; end
class ParseError < RuntimeError; end
end
+
+ class InvalidSearchQuery < ArgumentError; end
end
end
diff --git a/lib/chef/search/query.rb b/lib/chef/search/query.rb
index 274c5dfd23..cc43efe1b1 100644
--- a/lib/chef/search/query.rb
+++ b/lib/chef/search/query.rb
@@ -23,6 +23,7 @@ require 'chef/node'
require 'chef/role'
require 'chef/data_bag'
require 'chef/data_bag_item'
+require 'chef/exceptions'
class Chef
class Search
@@ -85,8 +86,8 @@ class Chef
# {"ip_address":"127.0.0.1", "ruby_version": "1.9.3"}
#
def search(type, query='*:*', *args, &block)
- raise ArgumentError, "Type must be a string or a symbol!" unless (type.kind_of?(String) || type.kind_of?(Symbol))
- raise ArgumentError, "Invalid number of arguments!" if (args.size > 3)
+ validate_type(type)
+ validate_args(args)
scrubbed_args = Hash.new
@@ -111,6 +112,20 @@ class Chef
end
private
+ def validate_type(t)
+ unless t.kind_of?(String) || t.kind_of?(Symbol)
+ msg = "Invalid search object type #{t.inspect} (#{t.class}), must be a String or Symbol." +
+ "Useage: search(:node, QUERY, [OPTIONAL_ARGS])" +
+ " `knife search environment QUERY (options)`"
+ raise Chef::Exceptions::InvalidSearchQuery, msg
+ end
+ end
+
+ def validate_args(a)
+ max_args = 3
+ raise Chef::Exceptions::InvalidSearchQuery, "Too many arguments! (#{a.size} for <= #{max_args})" if a.size > max_args
+ end
+
def escape(s)
s && URI.escape(s.to_s)
end
@@ -119,8 +134,6 @@ class Chef
# (formerly known as 'partial search').
# Also args should never be nil, but that is required for Ruby 1.8 compatibility
def do_search(type, query="*:*", args=nil, &block)
- raise ArgumentError, "Type must be a string or a symbol!" unless (type.kind_of?(String) || type.kind_of?(Symbol))
-
query_string = create_query_string(type, query, args)
response = call_rest_service(query_string, args)
unless block.nil?
diff --git a/spec/unit/search/query_spec.rb b/spec/unit/search/query_spec.rb
index 8a890898d3..c7388a6234 100644
--- a/spec/unit/search/query_spec.rb
+++ b/spec/unit/search/query_spec.rb
@@ -135,7 +135,7 @@ describe Chef::Search::Query do
it "should accept a type as the first argument" do
lambda { query.search("node") }.should_not raise_error
lambda { query.search(:node) }.should_not raise_error
- lambda { query.search(Hash.new) }.should raise_error(ArgumentError)
+ lambda { query.search(Hash.new) }.should raise_error(Chef::Exceptions::InvalidSearchQuery, /(Hash)/)
end
it "should query for every object of a type by default" do
@@ -164,7 +164,8 @@ describe Chef::Search::Query do
end
it "should throw an exception if you pass to many options" do
- lambda { query.search(:node, "platform:rhel", "id desc", 2, 40, "wrong") }.should raise_error(ArgumentError)
+ lambda { query.search(:node, "platform:rhel", "id desc", 2, 40, "wrong") }
+ .should raise_error(Chef::Exceptions::InvalidSearchQuery, "Too many arguments! (4 for <= 3)")
end
it "should return the raw rows, start, and total if no block is passed" do