Skip to content

Commit 30355c5

Browse files
committed
Add error on throw
1 parent facadf8 commit 30355c5

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

packages/shell-api/src/collection.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export default class Collection extends ShellApiWithMongoClass {
164164
async aggregate(
165165
pipeline: Document[],
166166
options: Document & { explain: ExplainVerbosityLike }
167-
): Promise<Document>;
167+
): Promise<AggregationCursor>;
168168
async aggregate(...stages: Document[]): Promise<AggregationCursor>;
169169
@returnsPromise
170170
@returnType('AggregationCursor')
@@ -191,7 +191,10 @@ export default class Collection extends ShellApiWithMongoClass {
191191
this._database._name,
192192
this._name,
193193
pipeline,
194-
{ ...(await this._database._baseOptions()), ...aggOptions },
194+
{
195+
...(await this._database._baseOptions()),
196+
...aggOptions,
197+
},
195198
dbOptions
196199
);
197200
const cursor = new AggregationCursor(this._mongo, providerCursor);

packages/shell-api/src/database.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,17 @@ describe('Database', function () {
390390
serviceProviderCursor = stubInterface<ServiceProviderAggCursor>();
391391
});
392392

393+
it('throws if the given argument is not an array', async function () {
394+
let caughtError: MongoshInvalidInputError | undefined;
395+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
396+
await database.aggregate({} as any).catch((err) => {
397+
caughtError = err;
398+
});
399+
expect(caughtError?.message).contains(
400+
'Aggregate pipeline argument must be an array'
401+
);
402+
});
403+
393404
it('calls serviceProvider.aggregateDb with pipleline and options', async function () {
394405
await database.aggregate([{ $piplelineStage: {} }], { options: true });
395406

packages/shell-api/src/database.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,14 @@ export default class Database extends ShellApiWithMongoClass {
432432
);
433433
}
434434
assertArgsDefinedType([pipeline], [true], 'Database.aggregate');
435+
436+
if (!Array.isArray(pipeline)) {
437+
throw new MongoshInvalidInputError(
438+
'Aggregate pipeline argument must be an array',
439+
CommonErrors.InvalidArgument
440+
);
441+
}
442+
435443
this._emitDatabaseApiCall('aggregate', { options, pipeline });
436444

437445
const { aggOptions, dbOptions, explain } = adaptAggregateOptions(options);
@@ -1429,6 +1437,7 @@ export default class Database extends ShellApiWithMongoClass {
14291437
CommonErrors.CommandFailed
14301438
);
14311439
}
1440+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
14321441
for (const cmdDescription of Object.values(result.commands) as Document[]) {
14331442
if ('slaveOk' in cmdDescription) {
14341443
cmdDescription.secondaryOk = cmdDescription.slaveOk;

0 commit comments

Comments
 (0)