Skip to content

Conversation

addaleax
Copy link
Collaborator

(Includes #809 for now, until that is merged.)

Handle exceptions from callbacks passed to cursor.map() and
cursor.forEach().

Since cursor.forEach() returns a Promise, use that to propagate
the error to the user.

For cursor.map(), this is a bit trickier, since the method itself
does not return any results to the user. Therefore, we add checks
before and after accessing cursor methods that read data which forward
the exception if one has occurred.

Both of these feel like situations that the driver should take care of
explicitly, so NODE tickets have been opened for both cases.

(One alternative would have been to add async context/Node.js domain
propagation to these methods. However, I decided against that, because
that solution would be a) Node.js-specific, and b) because it would
still lead to undesirable results, like the Promise returned by
.forEach() never resolving.)

Handle exceptions from callbacks passed to `cursor.map()` and
`cursor.forEach()`.

Since `cursor.forEach()` returns a Promise, use that to propagate
the error to the user.

For `cursor.map()`, this is a bit trickier, since the method itself
does not return any results to the user. Therefore, we add checks
before and after accessing cursor methods that read data which forward
the exception if one has occurred.

Both of these feel like situations that the driver should take care of
explicitly, so NODE tickets have been opened for both cases.

(One alternative would have been to add async context/Node.js domain
propagation to these methods. However, I decided against that, because
that solution would be a) Node.js-specific, and b) because it would
still lead to undesirable results, like the Promise returned by
`.forEach()` never resolving.)
// Ideally, this kind of code could be lifted into the driver (NODE-3231 and
// NODE-3232 are the tickets for that).
async _withCheckMapError<Ret>(fn: () => Ret): Promise<Ret> {
if (this._mapError) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice trick! is also pretty clear, i thought it would have been more hacky

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah … would still be nice if the driver had something here :)

@addaleax addaleax merged commit 8548f05 into master Apr 28, 2021
@addaleax addaleax deleted the 703-dev branch April 28, 2021 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants