summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSteven Danna <steve@opscode.com>2014-11-15 18:50:58 +0000
committerLamont Granquist <lamont@scriptkiddie.org>2016-07-19 10:14:11 -0700
commitf310cc0f92158f3e5f83193a88203636404c8e0d (patch)
treebedd55f171a4455cb6a28beada5d3f6d521c3ee1 /lib
parentdc14c7f2c362b538b85dff3c5f0f5451d100da62 (diff)
downloadchef-zero-f310cc0f92158f3e5f83193a88203636404c8e0d.tar.gz
Return HTTP 400 Bad Request on Solr parse errorlcg/rebase-101
Previously, chef-zero would return an HTTP 500 Internal Server Error when the Solr parser failed. Now, we return HTTP 400 with an error message identical to that of the Erchef server. Returning HTTP 400 also improves the end-user experience since knife returns more sensible error messages. Before this patch: > knife search nodes 'recipes:bar::bar' ERROR: Server returned error 500 for http://127.0.0.1:8889/search... After: > knife search nodes 'recipes:bar::bar' ERROR: knife search failed: invalid search query: 'recipes:bar::bar'
Diffstat (limited to 'lib')
-rw-r--r--lib/chef_zero/endpoints/search_endpoint.rb10
-rw-r--r--lib/chef_zero/solr/solr_parser.rb4
2 files changed, 13 insertions, 1 deletions
diff --git a/lib/chef_zero/endpoints/search_endpoint.rb b/lib/chef_zero/endpoints/search_endpoint.rb
index a9ad2bf..63d7f4a 100644
--- a/lib/chef_zero/endpoints/search_endpoint.rb
+++ b/lib/chef_zero/endpoints/search_endpoint.rb
@@ -14,6 +14,8 @@ module ChefZero
results = search(request, orgname)
results['rows'] = results['rows'].map { |name,uri,value,search_value| value }
json_response(200, results)
+ rescue ChefZero::Solr::ParseError
+ bad_search_request(request)
end
def post(request)
@@ -43,10 +45,18 @@ module ChefZero
'start' => full_results['start'],
'total' => full_results['total']
})
+ rescue ChefZero::Solr::ParseError
+ bad_search_request(request)
end
private
+ def bad_search_request(request)
+ query_string = request.query_params['q']
+ resp = {"error" => ["invalid search query: '#{query_string}'"]}
+ json_response(400, resp)
+ end
+
def search_container(request, index, orgname)
relative_parts, normalize_proc = case index
when 'client'
diff --git a/lib/chef_zero/solr/solr_parser.rb b/lib/chef_zero/solr/solr_parser.rb
index 936ead1..99da5fe 100644
--- a/lib/chef_zero/solr/solr_parser.rb
+++ b/lib/chef_zero/solr/solr_parser.rb
@@ -7,6 +7,8 @@ require 'chef_zero/solr/query/subquery'
module ChefZero
module Solr
+ class ParseError < RuntimeError; end
+
class SolrParser
def initialize(query_string)
@query_string = query_string
@@ -114,7 +116,7 @@ module ChefZero
end
def parse_error(token, str)
- raise "Error on token '#{token}' at #{@index} of '#{@query_string}': #{str}"
+ raise ChefZero::Solr::ParseError, "Error on token '#{token}' at #{@index} of '#{@query_string}': #{str}"
end
def read_single_expression