Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions spec/ParseQuery.hint.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,24 @@ describe_only_db('mongo')('Parse.Query hint', () => {
expect(explain.queryPlanner.winningPlan.inputStage.inputStage.indexName).toBe('_id_');
});

it('query find with invalid hint returns invalid query error', async () => {
const object = new TestObject();
await object.save();

const query = new Parse.Query(TestObject);
query.equalTo('objectId', object.id);
query.hint('missing_index');

try {
await query.find({ useMasterKey: true });
fail('Expected query.find to fail');
} catch (error) {
expect(error.code).toBe(Parse.Error.INVALID_QUERY);
expect(error.message.toLowerCase()).toContain('hint');
expect(error.code).not.toBe(Parse.Error.INTERNAL_SERVER_ERROR);
}
});

it_only_mongodb_version('>=7')('query aggregate with hint (rest)', async () => {
const object = new TestObject({ foo: 'bar' });
await object.save();
Expand Down
22 changes: 22 additions & 0 deletions src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ function isTransientError(error) {
return false;
}

function isInvalidHintError(error) {
if (!error || typeof error.message !== 'string') {
return false;
}

const message = error.message.toLowerCase();
const hasHintContext = message.includes('hint');
const hasIndexContext =
message.includes('index') ||
message.includes('badvalue') ||
error.code === 2 ||
error.code === 27 ||
error.codeName === 'BadValue' ||
error.codeName === 'IndexNotFound';

return hasHintContext && hasIndexContext;
}

const storageAdapterAllCollections = mongoAdapter => {
return mongoAdapter
.connect()
Expand Down Expand Up @@ -293,6 +311,10 @@ export class MongoStorageAdapter implements StorageAdapter {
throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, 'Database error');
}

if (isInvalidHintError(error)) {
throw new Parse.Error(Parse.Error.INVALID_QUERY, `Invalid hint: ${error.message}`);
}

throw error;
}

Expand Down
Loading