YUKI Hiroshi
null+****@clear*****
Fri Aug 24 18:24:30 JST 2012
YUKI Hiroshi 2012-08-24 18:24:30 +0900 (Fri, 24 Aug 2012) New Revision: e9846d3ba24347365f45057ba870305f8ecdc696 https://github.com/groonga/gcs/commit/e9846d3ba24347365f45057ba870305f8ecdc696 Log: Throw an error when the field is unsearchable Modified files: lib/api/2011-02-01/search.js lib/bq-translator.js Modified: lib/api/2011-02-01/search.js (+4 -1) =================================================================== --- lib/api/2011-02-01/search.js 2012-08-24 18:01:43 +0900 (96ff15a) +++ lib/api/2011-02-01/search.js 2012-08-24 18:24:30 +0900 (d4f10cd) @@ -103,9 +103,10 @@ function translateQueryToBooleanQuery(query) { return "'" + query.replace(/(['\\])/g, '\\$1') + "'"; } +var dummyRid = '000000000000000000000000000000000000000000000000000000000000000'; + exports.createHandler = function(context) { return function(request, response) { - var dummyRid = '000000000000000000000000000000000000000000000000000000000000000'; var startedAt = new Date(); var domain = new Domain(request, context); var query = request.query.q || ''; @@ -154,6 +155,8 @@ exports.createHandler = function(context) { var errorData = { rid: dummyRid }; if (error.message == 'validation error') { errorData.messages = [error.data]; + } else if (error.message == 'unsearchable field') { + errorData.message = 'unsearchable field '+error.fieldName; } else { errorData.message = 'Invalid bq value: ' + (error.message || error); } Modified: lib/bq-translator.js (+70 -53) =================================================================== --- lib/bq-translator.js 2012-08-24 18:01:43 +0900 (8245d16) +++ lib/bq-translator.js 2012-08-24 18:24:30 +0900 (4f23a84) @@ -31,24 +31,33 @@ BooleanQueryTranslator.prototype = { expression = this.translateExpression(); } if (this.offset != this.query.length) { - this.throwTranslateError("garbages exist after valid boolean query"); + this.throwSyntaxError("garbages exist after valid boolean query"); } return expression; }, - throwTranslateError: function(detail) { - var message = ""; - message += "<"; + throwInternalError: function(message) { + var error = new Error('internal error'); + error.data = message; + throw error; + }, + throwSyntaxError: function(message) { + var detail = ""; + detail += "<"; if (this.offset == this.query.length) { - message += this.query; - message += "||"; + detail += this.query; + detail += "||"; } else { - message += this.query.substring(0, this.offset); - message += "|" + this.query[this.offset] + "|"; - message += this.query.substring(this.offset + 1); + detail += this.query.substring(0, this.offset); + detail += "|" + this.query[this.offset] + "|"; + detail += this.query.substring(this.offset + 1); } - message += ">"; - message += ": " + detail; - throw new Error(message); + detail += ">"; + + this.throwValidationError( + 'CS-InvalidMatchSetExpression', + '[WARNING] Syntax error in match set expression: ' + + message + ' ' + detial + ); }, throwValidationError: function(code, message) { var error = new Error('validation error'); @@ -59,6 +68,11 @@ BooleanQueryTranslator.prototype = { }; throw error; }, + throwUnsearchableError: function(fieldName) { + var error = new Error('unsearchable field'); + error.fieldName = fieldName; + throw error; + }, skipSpaces: function() { for (; this.offset < this.query.length; this.offset++) { if (this.query[this.offset] != " ") { @@ -68,7 +82,7 @@ BooleanQueryTranslator.prototype = { }, translateGroup: function() { if (this.query[this.offset] != "(") { - this.throwTranslateError("open parenthesis is missing"); + this.throwSyntaxError("open parenthesis is missing"); } this.offset++; @@ -86,8 +100,8 @@ BooleanQueryTranslator.prototype = { expression = this.translateExpression(); this.skipSpaces(); if (this.query[this.offset] != ")") { - this.throwTranslateError("close parenthesis is missing: " + - "operator:<label>"); + this.throwSyntaxError("close parenthesis is missing: " + + "operator:<label>"); } this.offset++; break; @@ -105,19 +119,19 @@ BooleanQueryTranslator.prototype = { break; default: this.offset = operatorEndOffset; - this.throwTranslateError("unknown operator: <" + operator + ">"); + this.throwSyntaxError("unknown operator: <" + operator + ">"); break; } return expression; } else if (character == ")") { - this.throwTranslateError("operator is missing"); + this.throwSyntaxError("operator is missing"); } else { - this.throwTranslateError("invalid operator character: " + - "<" + character + ">"); + this.throwSyntaxError("invalid operator character: " + + "<" + character + ">"); } } - this.throwTranslateError("close parenthesis is missing"); + this.throwSyntaxError("close parenthesis is missing"); }, translateGroupField: function() { var field = ""; @@ -131,25 +145,25 @@ BooleanQueryTranslator.prototype = { this.skipSpaces(); var character = this.query[this.offset]; if (character != ")") { - this.throwTranslateError("a garbage character after value: " + - "<" + character + ">"); + this.throwSyntaxError("a garbage character after value: " + + "<" + character + ">"); } this.offset++; return expression; } else if (character == ")") { if (field.length == 0) { - this.throwTranslateError("field is missing"); + this.throwSyntaxError("field is missing"); } else { - this.throwTranslateError("field value is missing: " + - "field:<" + field + ">"); + this.throwSyntaxError("field value is missing: " + + "field:<" + field + ">"); } } else { - this.throwTranslateError("invalid field character: " + - "<" + character + ">"); + this.throwSyntaxError("invalid field character: " + + "<" + character + ">"); } } - this.throwTranslateError("close parenthesis is missing: operator:<field>"); + this.throwSyntaxError("close parenthesis is missing: operator:<field>"); }, translateGroupFilter: function() { var field = ""; @@ -163,25 +177,25 @@ BooleanQueryTranslator.prototype = { this.skipSpaces(); var character = this.query[this.offset]; if (character != ")") { - this.throwTranslateError("a garbage character after value: " + - "<" + character + ">"); + this.throwSyntaxError("a garbage character after value: " + + "<" + character + ">"); } this.offset++; return expression; } else if (character == ")") { if (field.length == 0) { - this.throwTranslateError("field is missing"); + this.throwSyntaxError("field is missing"); } else { - this.throwTranslateError("field value is missing: " + - "field:<" + field + ">"); + this.throwSyntaxError("field value is missing: " + + "field:<" + field + ">"); } } else { - this.throwTranslateError("invalid field character: " + - "<" + character + ">"); + this.throwSyntaxError("invalid field character: " + + "<" + character + ">"); } } - this.throwTranslateError("close parenthesis is missing: operator:<filter>"); + this.throwSyntaxError("close parenthesis is missing: operator:<filter>"); }, translateGroupSetOperation: function(label, setOperator) { var expressions = []; @@ -195,8 +209,8 @@ BooleanQueryTranslator.prototype = { } } - this.throwTranslateError("close parenthesis is missing: " + - "operator:<" + label + ">"); + this.throwSyntaxError("close parenthesis is missing: " + + "operator:<" + label + ">"); }, translateExpression: function() { if (this.query[this.offset] == "(") { @@ -215,7 +229,7 @@ BooleanQueryTranslator.prototype = { field += character; } if (this.query[this.offset] != ":") { - this.throwTranslateError("field value separator is missing"); + this.throwSyntaxError("field value separator is missing"); } this.offset++; } @@ -227,11 +241,11 @@ BooleanQueryTranslator.prototype = { return this.translateExpressionValueUnsignedInteger(field); } - this.throwTranslateError("invalid value: field:<" + field + ">"); + this.throwSyntaxError("invalid value: field:<" + field + ">"); }, translateExpressionValueString: function(field) { if (this.query[this.offset] != "'") { - this.throwTranslateError("open single quote for string value is missing"); + this.throwSyntaxError("open single quote for string value is missing"); } this.offset++; @@ -275,15 +289,15 @@ BooleanQueryTranslator.prototype = { } else if (character == "\\") { this.offset++; if (this.offset == this.query.length) { - this.throwTranslateError("escaped character is missing " + - "in string value"); + this.throwSyntaxError("escaped character is missing " + + "in string value"); } character = this.query[this.offset]; value += character; } else if (character == "\"") { if (value.length > 0) { - this.throwTranslateError("operator is missing: " + - "keyword:<" + value + ">"); + this.throwSyntaxError("operator is missing: " + + "keyword:<" + value + ">"); } tokens.push(this.translateExpressionValueStringPhrase(field)); this.offset--; @@ -292,7 +306,7 @@ BooleanQueryTranslator.prototype = { } } - this.throwTranslateError("close single quote for string value is missing"); + this.throwSyntaxError("close single quote for string value is missing"); }, translateExpressionValueStringKeyword: function(field, value) { var operator; @@ -309,7 +323,7 @@ BooleanQueryTranslator.prototype = { }, translateExpressionValueStringPhrase: function(field) { if (this.query[this.offset] != "\"") { - this.throwTranslateError("open double quote for phrase value is missing"); + this.throwSyntaxError("open double quote for phrase value is missing"); } this.offset += 1; @@ -319,7 +333,7 @@ BooleanQueryTranslator.prototype = { if (character == "\\") { this.offset++; if (this.offset == this.query.length) { - this.throwTranslateError("escaped character is missing in phrase"); + this.throwSyntaxError("escaped character is missing in phrase"); } character = this.query[this.offset]; value += character; @@ -339,7 +353,7 @@ BooleanQueryTranslator.prototype = { } } - this.throwTranslateError("close double quote for phrase is missing"); + this.throwSyntaxError("close double quote for phrase is missing"); }, translateExpressionValueUnsignedInteger: function(field) { var is_range = false; @@ -367,7 +381,7 @@ BooleanQueryTranslator.prototype = { if (is_range) { var expressions = []; if (min.length == 0 && max.length == 0) { - this.throwTranslateError("both min and max are missing"); + this.throwSyntaxError("both min and max are missing"); } if (min.length > 0) { expressions.push(this.constructBinaryOperation(field, ">=", min)); @@ -396,10 +410,10 @@ BooleanQueryTranslator.prototype = { return field + " " + operator + " " + value; } else { if (!this.defaultFieldNames) { - this.throwTranslateError("default fields are missing"); + this.throwInternalError("default fields are missing"); } if (this.defaultFieldNames.length == 0) { - this.throwTranslateError("no default field"); + this.throwInternalError("no default field"); } var expressions = this.defaultFieldNames.map(function (field) { return this.constructBinaryOperation(field, operator, value); @@ -417,13 +431,16 @@ BooleanQueryTranslator.prototype = { var field; if (this.domain && fieldName) { field = this.domain.getIndexField(fieldName); - if (!field.exists()) + if (!field.exists()) { this.throwValidationError( 'CS-UnknownFieldInMatchExpression', 'Field \'' + fieldName + '\' is not defined in the metadata for this ' + 'collection. All fields used in the match expression must be ' + 'defined in the metadata.' ); + } else if (!field.searchEnabled) { + this.throwUnsearchableError(fieldName); + } } if (!field) field = new IndexField(fieldName).setType("text"); -------------- next part -------------- HTML����������������������������...ダウンロード