Skip to content

Commit 9433c00

Browse files
committed
feat: Improve handling aggregate query errors
1 parent a9176d5 commit 9433c00

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

spec/ParseQuery.Aggregate.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,4 +1500,21 @@ describe('Parse.Query Aggregate testing', () => {
15001500
expect(results.length).toEqual(3);
15011501
await database.adapter.deleteAllClasses(false);
15021502
});
1503+
1504+
it_only_db('mongo')('aggregate handle mongodb errors', async () => {
1505+
const pipeline = [
1506+
{
1507+
$search: {
1508+
index: "default",
1509+
text: {
1510+
path: ["name"],
1511+
query: 'foo',
1512+
},
1513+
},
1514+
},
1515+
];
1516+
await expectAsync(new Parse.Query(TestObject).aggregate(pipeline)).toBeRejectedWith(
1517+
new Parse.Error(Parse.Error.INVALID_QUERY, 'Using $search and $vectorSearch aggregation stages requires additional configuration. Please connect to Atlas or an AtlasCLI local deployment to enable.For more information on how to connect, see https://dochub.mongodb.org/core/atlas-cli-deploy-local-reqs.')
1518+
);
1519+
});
15031520
});

src/Routers/AggregateRouter.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import ClassesRouter from './ClassesRouter';
55
import UsersRouter from './UsersRouter';
66

77
export class AggregateRouter extends ClassesRouter {
8-
handleFind(req) {
8+
async handleFind(req) {
99
const body = Object.assign(req.body || {}, ClassesRouter.JSONFromQuery(req.query));
1010
const options = {};
1111
if (body.distinct) {
@@ -31,24 +31,25 @@ export class AggregateRouter extends ClassesRouter {
3131
if (typeof body.where === 'string') {
3232
body.where = JSON.parse(body.where);
3333
}
34-
return rest
35-
.find(
34+
try {
35+
const response = await rest.find(
3636
req.config,
3737
req.auth,
3838
this.className(req),
3939
body.where,
4040
options,
4141
req.info.clientSDK,
4242
req.info.context
43-
)
44-
.then(response => {
45-
for (const result of response.results) {
46-
if (typeof result === 'object') {
47-
UsersRouter.removeHiddenProperties(result);
48-
}
43+
);
44+
for (const result of response.results) {
45+
if (typeof result === 'object') {
46+
UsersRouter.removeHiddenProperties(result);
4947
}
50-
return { response };
51-
});
48+
}
49+
return { response };
50+
} catch (e) {
51+
throw new Parse.Error(Parse.Error.INVALID_QUERY, e.message);
52+
}
5253
}
5354

5455
/* Builds a pipeline from the body. Originally the body could be passed as a single object,

0 commit comments

Comments
 (0)