Skip to content

Commit c283fea

Browse files
committed
refactor: Replace appropriate errors in src/db.ts with MongoInvalidArgumentError
1 parent 34389d1 commit c283fea

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/db.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
import { AggregationCursor } from './cursor/aggregation_cursor';
1010
import { Document, BSONSerializeOptions, resolveBSONOptions } from './bson';
1111
import { ReadPreference, ReadPreferenceLike } from './read_preference';
12-
import { MongoDriverError } from './error';
12+
import { MongoDriverError, MongoInvalidArgumentError } from './error';
1313
import { Collection, CollectionOptions } from './collection';
1414
import { ChangeStream, ChangeStreamOptions } from './change_stream';
1515
import * as CONSTANTS from './constants';
@@ -287,13 +287,13 @@ export class Db {
287287
*/
288288
aggregate(pipeline: Document[] = [], options?: AggregateOptions): AggregationCursor {
289289
if (arguments.length > 2) {
290-
throw new MongoDriverError('Third parameter to `db.aggregate()` must be undefined');
290+
throw new MongoInvalidArgumentError('Third parameter to `db.aggregate()` must be undefined');
291291
}
292292
if (typeof pipeline === 'function') {
293-
throw new MongoDriverError('`pipeline` parameter must not be function');
293+
throw new MongoInvalidArgumentError('`pipeline` parameter must not be function');
294294
}
295295
if (typeof options === 'function') {
296-
throw new MongoDriverError('`options` parameter must not be function');
296+
throw new MongoInvalidArgumentError('`options` parameter must not be function');
297297
}
298298

299299
return new AggregationCursor(
@@ -324,6 +324,7 @@ export class Db {
324324
if (!options) {
325325
options = {};
326326
} else if (typeof options === 'function') {
327+
// TODO: Replace this error with a more appropriate error class
327328
throw new MongoDriverError('The callback form of this helper has been removed.');
328329
}
329330
const finalOptions = resolveOptions(this, options);
@@ -724,15 +725,15 @@ export class Db {
724725
// Validate the database name
725726
function validateDatabaseName(databaseName: string) {
726727
if (typeof databaseName !== 'string')
727-
throw new MongoDriverError('database name must be a string');
728+
throw new MongoInvalidArgumentError('database name must be a string');
728729
if (databaseName.length === 0)
729-
throw new MongoDriverError('database name cannot be the empty string');
730+
throw new MongoInvalidArgumentError('database name cannot be the empty string');
730731
if (databaseName === '$external') return;
731732

732733
const invalidChars = [' ', '.', '$', '/', '\\'];
733734
for (let i = 0; i < invalidChars.length; i++) {
734735
if (databaseName.indexOf(invalidChars[i]) !== -1)
735-
throw new MongoDriverError(
736+
throw new MongoInvalidArgumentError(
736737
`database names cannot contain the character '${invalidChars[i]}'`
737738
);
738739
}

0 commit comments

Comments
 (0)