Skip to content

Commit 4870f4c

Browse files
committed
fix: map invalid mongo hint to invalid query error
1 parent 78859a9 commit 4870f4c

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

spec/ParseQuery.hint.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,25 @@ describe_only_db('mongo')('Parse.Query hint', () => {
153153
expect(explain.queryPlanner.winningPlan.inputStage.inputStage.indexName).toBe('_id_');
154154
});
155155

156+
it_only_mongodb_version('<5.1 || >=6')('query find with invalid hint returns invalid query error', async () => {
157+
const object = new TestObject();
158+
await object.save();
159+
160+
const query = new Parse.Query(TestObject);
161+
query.equalTo('objectId', object.id);
162+
query.hint('missing_index');
163+
164+
try {
165+
await query.find({ useMasterKey: true });
166+
fail('Expected query.find to fail');
167+
} catch (error) {
168+
expect(error.code).toBe(Parse.Error.INVALID_QUERY);
169+
expect(error.message.toLowerCase()).toContain('hint');
170+
expect(error.message.toLowerCase()).toContain('index');
171+
expect(error.code).not.toBe(Parse.Error.INTERNAL_SERVER_ERROR);
172+
}
173+
});
174+
156175
it_only_mongodb_version('>=7')('query aggregate with hint (rest)', async () => {
157176
const object = new TestObject({ foo: 'bar' });
158177
await object.save();

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ function isTransientError(error) {
5858
return false;
5959
}
6060

61+
function isInvalidHintError(error) {
62+
if (!error || typeof error.message !== 'string') {
63+
return false;
64+
}
65+
66+
const message = error.message.toLowerCase();
67+
const hasHintContext = message.includes('hint');
68+
const hasIndexContext =
69+
message.includes('index') ||
70+
message.includes('badvalue') ||
71+
error.code === 2 ||
72+
error.code === 27 ||
73+
error.codeName === 'BadValue' ||
74+
error.codeName === 'IndexNotFound';
75+
76+
return hasHintContext && hasIndexContext;
77+
}
78+
6179
const storageAdapterAllCollections = mongoAdapter => {
6280
return mongoAdapter
6381
.connect()
@@ -293,6 +311,10 @@ export class MongoStorageAdapter implements StorageAdapter {
293311
throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, 'Database error');
294312
}
295313

314+
if (isInvalidHintError(error)) {
315+
throw new Parse.Error(Parse.Error.INVALID_QUERY, `Invalid hint: ${error.message}`);
316+
}
317+
296318
throw error;
297319
}
298320

0 commit comments

Comments
 (0)