Navigation Menu

Skip to content

Commit

Permalink
search: handle syntax error in query syntax input
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Apr 10, 2014
1 parent 06c89fd commit 9917388
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/droonga/searcher.rb
Expand Up @@ -63,6 +63,18 @@ def initialize(attribute)
end
end

class SyntaxError < ErrorMessages::BadRequest
attr_reader :syntax
attr_reader :input
def initialize(syntax, input)
detail = {
"syntax" => syntax,
"input" => input,
}
super("Syntax error: syntax:<#{syntax}> input:<#{input}>", detail)
end
end

def initialize(context)
@context = context
end
Expand Down Expand Up @@ -262,7 +274,8 @@ def parse_condition_hash(source, expression, condition)
matchTo.parse(match_columns, :syntax => :script)
options[:default_column] = matchTo
end
if condition["query"]
query = condition["query"]
if query
options[:syntax] = :query
if condition["defaultOperator"]
default_operator_string = condition["defaultOperator"]
Expand All @@ -278,7 +291,11 @@ def parse_condition_hash(source, expression, condition)
if condition["allowColumn"]
options[:allow_column] = true
end
expression.parse(condition["query"], options)
begin
expression.parse(query, options)
rescue Groonga::SyntaxError
raise SyntaxError.new("query", query)
end
elsif condition["script"]
# "script" is ignored when "query" is also assigned.
options[:syntax] = :script
Expand Down
@@ -0,0 +1,34 @@
{
"datasets": {
"Droonga": {
"fact": "Memos",
"schema": {
"Memos": {
"type": "Hash",
"keyType": "ShortText",
"columns": {
"content": {
"type": "Scalar",
"valueType": "Text"
}
}
},
"Terms": {
"type": "PatriciaTrie",
"keyType": "ShortText",
"columns": {
"memos_index": {
"type": "Index",
"valueType": "Memos",
"indexOptions": {
"sources": [
"content"
]
}
}
}
}
}
}
}
}
48 changes: 48 additions & 0 deletions test/command/suite/search/condition/query/syntax_error.expected
@@ -0,0 +1,48 @@
{
"inReplyTo": "request-id",
"statusCode": 400,
"type": "search.result",
"body": {
"name": "SyntaxError",
"message": "Syntax error: syntax:<query> input:<(>",
"detail": {
"syntax": "query",
"input": "("
}
},
"errors": {
"sources0": {
"statusCode": 400,
"body": {
"name": "SyntaxError",
"message": "Syntax error: syntax:<query> input:<(>",
"detail": {
"syntax": "query",
"input": "("
}
}
},
"sources1": {
"statusCode": 400,
"body": {
"name": "SyntaxError",
"message": "Syntax error: syntax:<query> input:<(>",
"detail": {
"syntax": "query",
"input": "("
}
}
},
"sources2": {
"statusCode": 400,
"body": {
"name": "SyntaxError",
"message": "Syntax error: syntax:<query> input:<(>",
"detail": {
"syntax": "query",
"input": "("
}
}
}
}
}
33 changes: 33 additions & 0 deletions test/command/suite/search/condition/query/syntax_error.test
@@ -0,0 +1,33 @@
# -*- js -*-
#@require-catalog-version 2
#@disable-logging
{
"type": "add",
"dataset": "Droonga",
"body": {
"table": "Memos",
"key": "droonga",
"values": {
"content": "Droonga is fun!"
}
}
}
#@enable-logging
{
"type": "search",
"dataset": "Droonga",
"body": {
"queries": {
"memos": {
"source": "Memos",
"condition": {
"matchTo": ["content"],
"query": "("
},
"output": {
"elements": ["count"]
}
}
}
}
}

0 comments on commit 9917388

Please sign in to comment.