Skip to content

Commit

Permalink
Return ACS compatible error response from search API around unknown f…
Browse files Browse the repository at this point in the history
…ield
  • Loading branch information
piroor committed Aug 24, 2012
1 parent ae3897b commit 293fbb6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
29 changes: 18 additions & 11 deletions lib/api/2011-02-01/search.js
Expand Up @@ -80,18 +80,22 @@ function select(context, options, callback) {
}

function createErrorBody(options) {
var messages = [];
if (options.messages) {
messages = options.messages;
} else {
messages.push({
severity: 'fatal',
code: '',
message: options.message || ''
});
}
return {
error: 'info',
rid: options.rid,
'time-ms': options.elapsedTime || 0,
'cpu-time-ms': 0, // TODO
messages: [
{
severity: 'fatal',
code: '',
message: options.message || ''
}
]
messages: messages
};
}

Expand Down Expand Up @@ -147,10 +151,13 @@ exports.createHandler = function(context) {
try {
filters.push(translator.translate());
} catch (error) {
var body = createErrorBody({
rid: dummyRid,
message: 'Invalid bq value: ' + (error.message || error)
});
var errorData = { rid: dummyRid };
if (error.message == 'validation error') {
errorData.messages = [error.data];
} else {
errorData.message = 'Invalid bq value: ' + (error.message || error);
}
var body = createErrorBody(errorData);
return response.send(body, 400);
}
if (matchExpr.length > 0) {
Expand Down
22 changes: 18 additions & 4 deletions lib/bq-translator.js
Expand Up @@ -50,6 +50,15 @@ BooleanQueryTranslator.prototype = {
message += ": " + detail;
throw new Error(message);
},
throwValidationError: function(code, message) {
var error = new Error('validation error');
error.data = {
code: code,
severity: 'fatal',
message: message
};
throw error;
},
skipSpaces: function() {
for (; this.offset < this.query.length; this.offset++) {
if (this.query[this.offset] != " ") {
Expand Down Expand Up @@ -403,13 +412,18 @@ BooleanQueryTranslator.prototype = {
}
},
// Get an instance of "IndexField".
// Unknown field works as "text, searchable" field.
// Unknown field works as "text, searchable" field, if it works without domain.
getField: function(fieldName) {
var field;
if (this.domain) {
if (this.domain && fieldName) {
field = this.domain.getIndexField(fieldName);
if (field && !field.exists())
field.type = "text";
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.'
);
}
if (!field)
field = new IndexField(fieldName).setType("text");
Expand Down
11 changes: 11 additions & 0 deletions test/bq-translator.test.js
Expand Up @@ -157,6 +157,17 @@ suite('BoolanQueryTranslator', function() {
temporaryDatabase = utils.createTemporaryDatabase();
context = temporaryDatabase.get();
domain = new Domain('test', context).createSync();
[
'type',
'name',
'field',
'field1',
'field2',
'field3'
].forEach(function(field) {
domain.getIndexField(field).setType('text')
.setFacetEnabled(true).setSearchEnabled(true).createSync();
});
domain.getIndexField('literalfield').setType('literal')
.setFacetEnabled(true).setSearchEnabled(true).createSync();
});
Expand Down

0 comments on commit 293fbb6

Please sign in to comment.