Skip to content

Commit

Permalink
Don't 500 if search attribute is invalid
Browse files Browse the repository at this point in the history
In the case where a user, when providing a search, provides an attribute
constraint (-a flag) that is invalid, we currently throw a 500. An
invalid attribute constraint most commonly is specifying a nested hash
using dot-notation when one or more values in that constraint are, in
fact, not a hash. This change prevents the system from throwing a 500
and presents the user with a more correct result, that there is no value
for that attribute (because it doesn't exist).

For example, `knife search node "*:*" -a uptime.seconds` is invalid
because the key `'uptime'` does exist but it is a string, not a
hash. Thus, ej attempts to fetch the key `'seconds'` from that string,
throwing an error within ej for an invalid function clause. With this
change, the search will return just fine but show that there is no value
for `uptime.seconds`.

Signed-off-by: Tom Duffield <[email protected]>
  • Loading branch information
tduffield committed Sep 6, 2016
1 parent dea545b commit c19cb94
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/oc_erchef/apps/oc_chef_wm/src/chef_wm_search.erl
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,16 @@ make_query_from_params(Req) ->
extract_path(_Item, []) ->
null;
extract_path(Item, Path) ->
ej:get(list_to_tuple(Path), Item, null).
try ej:get(list_to_tuple(Path), Item, null) of
Result -> Result
catch
%% Don't throw a 500 when the user input is invalid
%% This typically happens when the user provides an invalid attribute
%% key, typically trying to access a value as a hash that is actually
%% a string (i.e. uptime.seconds)
error:function_clause -> null;
Error:Reason -> {Error, Reason}
end.

make_search_results(BulkGetFun, Ids, BatchSize, Start, NumFound) ->
Ans0 = search_result_start(Start, NumFound),
Expand Down

0 comments on commit c19cb94

Please sign in to comment.