diff options
author | Claire McQuin <claire@getchef.com> | 2014-09-04 12:37:45 -0700 |
---|---|---|
committer | Claire McQuin <claire@getchef.com> | 2014-09-04 15:52:47 -0700 |
commit | 8022a0d89a85787a4b6550428ab26ae37b09b8c0 (patch) | |
tree | a7bcf6ff5d7e677c2c84724bc83519d19bd6c414 /lib/chef/search/query.rb | |
parent | 260278d883c86dfab0c9bb933bdbe91cae752a3f (diff) | |
download | chef-8022a0d89a85787a4b6550428ab26ae37b09b8c0.tar.gz |
Add InvalidSearchQuery exception for malformed searches.
Diffstat (limited to 'lib/chef/search/query.rb')
-rw-r--r-- | lib/chef/search/query.rb | 21 |
1 files changed, 17 insertions, 4 deletions
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? |