Skip to content

Commit 1a16b7e

Browse files
fix(NODE-4518): error message with invalid authMechanism is provided (#undefined)
1 parent f42cb3d commit 1a16b7e

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/connection_string.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ export function parseOptions(
403403
) {
404404
// If authSource was explicitly given and its incorrect, we error
405405
throw new MongoParseError(
406-
`${mongoOptions.credentials} can only have authSource set to '$external'`
406+
`authMechanism ${mongoOptions.credentials.mechanism} requires an authSource of '$external'`
407407
);
408408
}
409409

test/unit/connection_string.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,18 @@ describe('Connection String', function () {
237237
expect(options).to.not.have.property('credentials');
238238
});
239239

240+
for (const mechanism of ['GSSAPI', 'MONGODB-X509']) {
241+
context(`when the authMechanism is ${mechanism} and authSource is NOT $external`, function () {
242+
it('throws a MongoParseError', function () {
243+
expect(() =>
244+
parseOptions(`mongodb+srv://hostname/?authMechanism=${mechanism}&authSource=invalid`)
245+
)
246+
.to.throw(MongoParseError)
247+
.to.match(/requires an authSource of '\$external'/);
248+
});
249+
});
250+
}
251+
240252
it('should omit credentials and not throw a MongoAPIError if the only auth related option is authSource', async () => {
241253
// The error we're looking to **not** see is
242254
// `new MongoInvalidArgumentError('No AuthProvider for ${credentials.mechanism} defined.')`

0 commit comments

Comments
 (0)