-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(NODE-5484)!: mark MongoError for internal use and remove Node14 cause assignment logic #3800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
a8653ba
d504317
d2012c5
ea5ea72
53491bd
55f487c
ccb0866
be7c2f9
85b02a5
4b1ad15
aac86bc
48f7e15
6618575
ae76093
1d08c4b
2a58338
12d8aa7
484519d
d8ee589
0838e97
7120ef7
414d53a
b51ad79
161d32b
564d117
aaa2ebe
4ca5acd
6511534
65c7767
4ec65c2
56920da
9d01de6
25ad3a4
b0d687a
2cfa31d
b818f3b
d24285b
c942e9f
7553a6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,6 +108,10 @@ export interface ErrorDescription extends Document { | |
errInfo?: Document; | ||
} | ||
|
||
interface OptionsWithCause { | ||
cause?: Error; | ||
} | ||
|
||
function isAggregateError(e: Error): e is Error & { errors: Error[] } { | ||
return 'errors' in e && Array.isArray(e.errors); | ||
} | ||
|
@@ -130,16 +134,10 @@ export class MongoError extends Error { | |
code?: number | string; | ||
topologyVersion?: TopologyVersion; | ||
connectionGeneration?: number; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
cause?: Error; // depending on the node version, this may or may not exist on the base | ||
|
||
constructor(message: string | Error) { | ||
super(MongoError.buildErrorMessage(message)); | ||
if (message instanceof Error) { | ||
this.cause = message; | ||
} | ||
|
||
/** @internal */ | ||
constructor(message: string | Error, options?: OptionsWithCause) { | ||
super(MongoError.buildErrorMessage(message), options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this comment suggesting we fix the tests or that we revert the change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either, I don't feel strongly. I don't see test failures which is intriguing, maybe because the tests are still just passing in an error as the first argument? |
||
this[kErrorLabels] = new Set(); | ||
} | ||
|
||
|
@@ -198,6 +196,7 @@ export class MongoServerError extends MongoError { | |
ok?: number; | ||
[key: string]: any; | ||
|
||
/** @internal */ | ||
constructor(message: ErrorDescription) { | ||
super(message.message || message.errmsg || message.$err || 'n/a'); | ||
if (message.errorLabels) { | ||
|
@@ -222,6 +221,7 @@ export class MongoServerError extends MongoError { | |
* @category Error | ||
*/ | ||
export class MongoDriverError extends MongoError { | ||
/** @internal */ | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
|
@@ -242,6 +242,7 @@ export class MongoDriverError extends MongoError { | |
*/ | ||
|
||
export class MongoAPIError extends MongoDriverError { | ||
/** @internal */ | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
|
@@ -262,6 +263,7 @@ export class MongoAPIError extends MongoDriverError { | |
* @category Error | ||
*/ | ||
export class MongoRuntimeError extends MongoDriverError { | ||
/** @internal */ | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
|
@@ -279,6 +281,7 @@ export class MongoRuntimeError extends MongoDriverError { | |
* @category Error | ||
*/ | ||
export class MongoBatchReExecutionError extends MongoAPIError { | ||
/** @internal */ | ||
constructor(message = 'This batch has already been executed, create new batch to execute') { | ||
super(message); | ||
} | ||
|
@@ -296,6 +299,7 @@ export class MongoBatchReExecutionError extends MongoAPIError { | |
* @category Error | ||
*/ | ||
export class MongoDecompressionError extends MongoRuntimeError { | ||
/** @internal */ | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
|
@@ -313,6 +317,7 @@ export class MongoDecompressionError extends MongoRuntimeError { | |
* @category Error | ||
*/ | ||
export class MongoNotConnectedError extends MongoAPIError { | ||
/** @internal */ | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
|
@@ -330,6 +335,7 @@ export class MongoNotConnectedError extends MongoAPIError { | |
* @category Error | ||
*/ | ||
export class MongoTransactionError extends MongoAPIError { | ||
/** @internal */ | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
|
@@ -347,6 +353,7 @@ export class MongoTransactionError extends MongoAPIError { | |
* @category Error | ||
*/ | ||
export class MongoExpiredSessionError extends MongoAPIError { | ||
/** @internal */ | ||
constructor(message = 'Cannot use a session that has ended') { | ||
super(message); | ||
} | ||
|
@@ -364,6 +371,7 @@ export class MongoExpiredSessionError extends MongoAPIError { | |
* @category Error | ||
*/ | ||
export class MongoKerberosError extends MongoRuntimeError { | ||
/** @internal */ | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
|
@@ -381,6 +389,7 @@ export class MongoKerberosError extends MongoRuntimeError { | |
* @category Error | ||
*/ | ||
export class MongoAWSError extends MongoRuntimeError { | ||
/** @internal */ | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
|
@@ -398,6 +407,7 @@ export class MongoAWSError extends MongoRuntimeError { | |
* @category Error | ||
*/ | ||
export class MongoAzureError extends MongoRuntimeError { | ||
/** @internal */ | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
|
@@ -414,6 +424,7 @@ export class MongoAzureError extends MongoRuntimeError { | |
* @category Error | ||
*/ | ||
export class MongoChangeStreamError extends MongoRuntimeError { | ||
/** @internal */ | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
|
@@ -430,6 +441,7 @@ export class MongoChangeStreamError extends MongoRuntimeError { | |
* @category Error | ||
*/ | ||
export class MongoTailableCursorError extends MongoAPIError { | ||
/** @internal */ | ||
constructor(message = 'Tailable cursor does not support this operation') { | ||
super(message); | ||
} | ||
|
@@ -445,6 +457,7 @@ export class MongoTailableCursorError extends MongoAPIError { | |
* @category Error | ||
*/ | ||
export class MongoGridFSStreamError extends MongoRuntimeError { | ||
/** @internal */ | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
|
@@ -462,6 +475,7 @@ export class MongoGridFSStreamError extends MongoRuntimeError { | |
* @category Error | ||
*/ | ||
export class MongoGridFSChunkError extends MongoRuntimeError { | ||
/** @internal */ | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
|
@@ -488,6 +502,7 @@ export class MongoGridFSChunkError extends MongoRuntimeError { | |
* @category Error | ||
*/ | ||
export class MongoUnexpectedServerResponseError extends MongoRuntimeError { | ||
/** @internal */ | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
|
@@ -505,6 +520,7 @@ export class MongoUnexpectedServerResponseError extends MongoRuntimeError { | |
* @category Error | ||
*/ | ||
export class MongoCursorInUseError extends MongoAPIError { | ||
/** @internal */ | ||
constructor(message = 'Cursor is already initialized') { | ||
super(message); | ||
} | ||
|
@@ -522,6 +538,7 @@ export class MongoCursorInUseError extends MongoAPIError { | |
* @category Error | ||
*/ | ||
export class MongoServerClosedError extends MongoAPIError { | ||
/** @internal */ | ||
constructor(message = 'Server is closed') { | ||
super(message); | ||
} | ||
|
@@ -538,6 +555,7 @@ export class MongoServerClosedError extends MongoAPIError { | |
* @category Error | ||
*/ | ||
export class MongoCursorExhaustedError extends MongoAPIError { | ||
/** @internal */ | ||
constructor(message?: string) { | ||
super(message || 'Cursor is exhausted'); | ||
} | ||
|
@@ -555,6 +573,7 @@ export class MongoCursorExhaustedError extends MongoAPIError { | |
* @category Error | ||
*/ | ||
export class MongoTopologyClosedError extends MongoAPIError { | ||
/** @internal */ | ||
constructor(message = 'Topology is closed') { | ||
super(message); | ||
} | ||
|
@@ -585,6 +604,7 @@ export class MongoNetworkError extends MongoError { | |
/** @internal */ | ||
[kBeforeHandshake]?: boolean; | ||
|
||
/** @internal */ | ||
constructor(message: string | Error, options?: MongoNetworkErrorOptions) { | ||
super(message); | ||
|
||
|
@@ -607,6 +627,7 @@ export class MongoNetworkError extends MongoError { | |
* mongodb-client-encryption has a dependency on this error with an instanceof check | ||
*/ | ||
export class MongoNetworkTimeoutError extends MongoNetworkError { | ||
/** @internal */ | ||
constructor(message: string, options?: MongoNetworkErrorOptions) { | ||
super(message, options); | ||
} | ||
|
@@ -622,6 +643,7 @@ export class MongoNetworkTimeoutError extends MongoNetworkError { | |
* @category Error | ||
*/ | ||
export class MongoParseError extends MongoDriverError { | ||
/** @internal */ | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
|
@@ -640,6 +662,7 @@ export class MongoParseError extends MongoDriverError { | |
* @category Error | ||
*/ | ||
export class MongoInvalidArgumentError extends MongoAPIError { | ||
/** @internal */ | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
|
@@ -658,6 +681,7 @@ export class MongoInvalidArgumentError extends MongoAPIError { | |
* @category Error | ||
*/ | ||
export class MongoCompatibilityError extends MongoAPIError { | ||
/** @internal */ | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
|
@@ -676,6 +700,7 @@ export class MongoCompatibilityError extends MongoAPIError { | |
* @category Error | ||
*/ | ||
export class MongoMissingCredentialsError extends MongoAPIError { | ||
/** @internal */ | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
|
@@ -692,9 +717,9 @@ export class MongoMissingCredentialsError extends MongoAPIError { | |
* @category Error | ||
*/ | ||
export class MongoMissingDependencyError extends MongoAPIError { | ||
constructor(message: string, { cause }: { cause?: Error } = {}) { | ||
/** @internal */ | ||
constructor(message: string) { | ||
super(message); | ||
if (cause) this.cause = cause; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What exactly is it that adding If you want to prevent users from creating instances of these classes, you may need to do something like export class MongoSomethingError extends MongoError {
private constructor(...) {}
/** @internal */
static create(...args) { return new this(...args); }
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're doing this in an attempt to communicate that these classes shouldn't be instantiated by users. It's a pattern we have a lot elsewhere in the driver that probably could use some work with regards to getting that message across. Wouldn't this solution also have the same effect? The user might not have documentation for the static There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I get the intention here, and I agree that it's a goal that's worth putting effort into. :) The problem is specifically with marking constructors as That's different with a static method; yeah, of course users could still call it by casting the class to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I see what you're saying now. I'll check with the team to see if we want to start with that new pattern here or deal with it in a separate ticket that takes care of this across the driver. Thanks for the clarification! |
||
|
||
override get name(): string { | ||
|
@@ -710,11 +735,12 @@ export class MongoSystemError extends MongoError { | |
/** An optional reason context, such as an error saved during flow of monitoring and selecting servers */ | ||
reason?: TopologyDescription; | ||
|
||
constructor(message: string, reason: TopologyDescription) { | ||
/** @internal */ | ||
constructor(message: string, reason: TopologyDescription, options?: OptionsWithCause) { | ||
if (reason && reason.error) { | ||
super(reason.error.message || reason.error); | ||
super(reason.error.message || reason.error, options); | ||
} else { | ||
super(message); | ||
super(message, options); | ||
} | ||
|
||
if (reason) { | ||
|
@@ -735,6 +761,7 @@ export class MongoSystemError extends MongoError { | |
* @category Error | ||
*/ | ||
export class MongoServerSelectionError extends MongoSystemError { | ||
/** @internal */ | ||
constructor(message: string, reason: TopologyDescription) { | ||
super(message, reason); | ||
} | ||
|
@@ -766,6 +793,7 @@ export class MongoWriteConcernError extends MongoServerError { | |
/** The result document (provided if ok: 1) */ | ||
result?: Document; | ||
|
||
/** @internal */ | ||
constructor(message: ErrorDescription, result?: Document) { | ||
if (result && Array.isArray(result.errorLabels)) { | ||
message.errorLabels = result.errorLabels; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* eslint-disable @typescript-eslint/no-restricted-imports */ | ||
import { expect } from 'chai'; | ||
|
||
import { | ||
MongoCryptAzureKMSRequestError, | ||
MongoCryptCreateDataKeyError, | ||
MongoCryptCreateEncryptedCollectionError, | ||
MongoCryptError, | ||
MongoCryptInvalidArgumentError | ||
} from '../../../src/client-side-encryption/errors'; | ||
import { MongoError } from '../../mongodb'; | ||
|
||
describe('ClientEncryption', function () { | ||
describe('Errors', function () { | ||
const errors = [ | ||
new MongoCryptAzureKMSRequestError(''), | ||
new MongoCryptCreateDataKeyError({ | ||
encryptedFields: {}, | ||
cause: new Error() | ||
}), | ||
new MongoCryptCreateEncryptedCollectionError({ | ||
encryptedFields: {}, | ||
cause: new Error() | ||
}), | ||
new MongoCryptError(''), | ||
new MongoCryptInvalidArgumentError('') | ||
]; | ||
|
||
for (const err of errors) { | ||
describe(err.name, function () { | ||
it('is subclass of MongoError', function () { | ||
expect(err).to.be.instanceOf(MongoError); | ||
}); | ||
}); | ||
} | ||
}); | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.