Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
make error required and move to ooptions
  • Loading branch information
baileympearson committed Apr 3, 2024
commit 44fba7e16761d04955b74d9a543d90bf218164ee
30 changes: 11 additions & 19 deletions src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ export function getKerberos(): Kerberos {
kerberos = makeErrorModule(
new MongoMissingDependencyError(
'Optional module `kerberos` not found. Please install it to enable kerberos authentication',
'kerberos',
{ cause: error }
{ cause: error, dependencyName: 'kerberos' }
)
);
}
Expand Down Expand Up @@ -69,8 +68,7 @@ export function getZstdLibrary(): ZStandardLib | { kModuleError: MongoMissingDep
ZStandard = makeErrorModule(
new MongoMissingDependencyError(
'Optional module `@mongodb-js/zstd` not found. Please install it to enable zstd compression',
'zstd',
{ cause: error }
{ cause: error, dependencyName: 'zstd' }
)
);
}
Expand Down Expand Up @@ -110,8 +108,7 @@ export function getAwsCredentialProvider():
new MongoMissingDependencyError(
'Optional module `@aws-sdk/credential-providers` not found.' +
' Please install it to enable getting aws credentials via the official sdk.',
'@aws-sdk/credential-providers',
{ cause: error }
{ cause: error, dependencyName: '@aws-sdk/credential-providers' }
)
);
}
Expand All @@ -132,8 +129,7 @@ export function getGcpMetadata(): GcpMetadata {
new MongoMissingDependencyError(
'Optional module `gcp-metadata` not found.' +
' Please install it to enable getting gcp credentials via the official sdk.',
'gcp-metadata',
{ cause: error }
{ cause: error, dependencyName: 'gcp-metadata' }
)
);
}
Expand All @@ -159,11 +155,10 @@ export function getSnappy(): SnappyLib | { kModuleError: MongoMissingDependencyE
// Ensure you always wrap an optional require in the try block NODE-3199
const value = require('snappy');
return value;
} catch (cause) {
} catch (error) {
const kModuleError = new MongoMissingDependencyError(
'Optional module `snappy` not found. Please install it to enable snappy compression',
'snappy',
{ cause }
{ cause: error, dependencyName: 'snappy' }
);
return { kModuleError };
}
Expand Down Expand Up @@ -194,11 +189,10 @@ export function getSocks(): SocksLib | { kModuleError: MongoMissingDependencyErr
// Ensure you always wrap an optional require in the try block NODE-3199
const value = require('socks');
return value;
} catch (cause) {
} catch (error) {
const kModuleError = new MongoMissingDependencyError(
'Optional module `socks` not found. Please install it to connections over a SOCKS5 proxy',
'socks',
{ cause }
{ cause: error, dependencyName: 'socks' }
);
return { kModuleError };
}
Expand Down Expand Up @@ -255,8 +249,7 @@ function loadAws4() {
aws4 = makeErrorModule(
new MongoMissingDependencyError(
'Optional module `aws4` not found. Please install it to enable AWS authentication',
'aws4',
{ cause: error }
{ cause: error, dependencyName: 'aws4' }
)
);
}
Expand All @@ -275,11 +268,10 @@ export function getMongoDBClientEncryption():
// Cannot be moved to helper utility function, bundlers search and replace the actual require call
// in a way that makes this line throw at bundle time, not runtime, catching here will make bundling succeed
mongodbClientEncryption = require('mongodb-client-encryption');
} catch (cause) {
} catch (error) {
const kModuleError = new MongoMissingDependencyError(
'Optional module `mongodb-client-encryption` not found. Please install it to use auto encryption or ClientEncryption.',
'mongodb-client-encryption',
{ cause }
{ cause: error, dependencyName: 'mongodb-client-encryption' }
);
return { kModuleError };
}
Expand Down
6 changes: 4 additions & 2 deletions src/encrypter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@ export class Encrypter {
throw new MongoMissingDependencyError(
'Auto-encryption requested, but the module is not installed. ' +
'Please add `mongodb-client-encryption` as a dependency of your project',
'mongodb-client-encryption',
{ cause: mongodbClientEncryption['kModuleError'] }
{
cause: mongodbClientEncryption['kModuleError'].cause,
dependencyName: 'mongodb-client-encryption'
}
);
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,8 @@ export class MongoMissingCredentialsError extends MongoAPIError {
*/
export class MongoMissingDependencyError extends MongoAPIError {
dependencyName: string;
override cause: Error;

/**
* **Do not use this constructor!**
*
Expand All @@ -1020,9 +1022,10 @@ export class MongoMissingDependencyError extends MongoAPIError {
*
* @public
**/
constructor(message: string, dependencyName: string, options: { cause: Error }) {
constructor(message: string, options: { cause: Error; dependencyName: string }) {
super(message, options);
this.dependencyName = dependencyName;
this.dependencyName = options.dependencyName;
this.cause = options.cause;
}

override get name(): string {
Expand Down