-
Notifications
You must be signed in to change notification settings - Fork 1.8k
refactor(NODE-3404): implement MongoRuntimeError children #2912
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
refactor(NODE-3404): implement MongoRuntimeError children #2912
Conversation
1bfa5fc
to
990c92c
Compare
Apart from requested changes, LGTM |
bc6fe74
to
0e77b80
Compare
if (compressorID < 0 || compressorID > Math.max(2)) { | ||
throw new MongoDriverError( | ||
throw new MongoDecompressionError( | ||
`Server sent message compressed using an unsupported compressor.` + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets make this one template string?
src/cmap/message_stream.ts
Outdated
callback( | ||
new MongoDriverError( | ||
new MongoDecompressionError( | ||
'Decompressing a compressed message from the server failed. The message is corrupt.' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like we could improve the message a bit here, a MongoDecompressError implies the first sentence maybe its worth being specific, we can workshop but something like: 'Message header size does not match message body size'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does 'Message body and message header must be the same length'
sound?
src/gridfs/download.ts
Outdated
expectedLength; | ||
return __handleError(stream, new MongoDriverError(errmsg)); | ||
errmsg = `ChunkIsWrongSize: Got unexpected length: ${buf.byteLength}, expected: ${expectedLength}`; | ||
return __handleError(stream, new MongoGridFSChunkError(errmsg)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__handleError is a oneliner (maybe it contained more at some point) can we just inline it everywhere?
return stream.emit(
GridFSBucketReadStream.ERROR,
new MongoGridFSChunkError(
`ChunkIsWrongSize: Got unexpected length: ${buf.byteLength}, expected: ${expectedLength}`
)
);
abort(callback?: Callback<void>): Promise<void> | void { | ||
const Promise = PromiseProvider.get(); | ||
let error: MongoDriverError; | ||
let error: MongoGridFSStreamError; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we refactor this to use maybePromise
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In hindsight, I think this whole thing is out of scope for this ticket. I think MongoStreamClosedError
, covered in NODE-3405, better covers the errors thrown here. I'll leave a TODO comment for that PR and revert this change.
MongoGridFSChunkError, | ||
MongoGridFSStreamError | ||
} from '../error'; | ||
import type { FindOptions } from '../operations/find'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's import changes here that aren't needed, I think you import sorting turned on in vscode? This is something we want to do as a lint fix down the line if you don't mind reverting these.
src/gridfs/download.ts
Outdated
@@ -1,14 +1,20 @@ | |||
import type { ObjectId } from 'bson'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be '../bson'
must've slipped through I'll look into linting for this.
src/connection_string.ts
Outdated
for (const compVal of values as string[]) { | ||
for (const c of compVal.split(',')) { | ||
if (['none', 'snappy', 'zlib'].includes(String(c))) { | ||
if (Object.values(VALID_COMPRESSION_MECHS).includes(String(c))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (Object.values(VALID_COMPRESSION_MECHS).includes(String(c))) { | |
if (Object.keys(Compressor).includes(String(c))) { |
We already have an enum Compressor
lets use that here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, didn't realize there was an existing enum already 😅
src/connection_string.ts
Outdated
} else { | ||
throw new MongoInvalidArgumentError( | ||
`${c} is not a valid compression mechanism. Must be 'none', 'snappy', or 'zlib'. ` | ||
`${c} is not a valid compression mechanism. Must be one of: ${enumToString( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess that would just make this ${Object.keys(Compressor)}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than this small but important detail, LGTM
changeStream.close(close); | ||
} else if (counter >= 3) { | ||
close(new Error('should not have received more than 2 events')); | ||
expect.fail('Should not have received more than 2 events'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original close function here served an important purpose: to prevent done from being called more than once; an expect.fail would throw an error, which would call done in whatever context is currently running, potentially causing unrelated tests to fail. tl;dr: passing the error explicitly to a wrapped done function (the close
function in this context), the way it was written originally, is the safest way to handle event listener errors in tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, I didn't realize that. This was helpful in understanding the tests a bit better. Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
once the fix daria mentions is in then this LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🎉
d2f57e8
to
5659654
Compare
Description
Replace instances of MongoDriverError with the appropriate children of MongoRuntimeError, listed below, as described in docs/errors.md.
Children of MongoRuntimeError used