Skip to content

Conversation

@AbdelrahmanHafez
Copy link
Collaborator

Summary

Fix bulkWrite() to call error post hooks when a pre-hook throws an error. Fixes #15881

Changes

  • Call execPost with { error } when bulkWrite pre-hook throws, matching insertMany behavior

Before

[ops, options] = await this.hooks.execPre(...).catch(err => {
  if (err instanceof Kareem.skipWrappedFunction) return [err];
  throw err;  // error post hooks not called
});

After

try {
  [ops, options] = await this.hooks.execPre(...);
} catch (err) {
  if (err instanceof Kareem.skipWrappedFunction) {
    ops = err;
  } else {
    await this.hooks.execPost('bulkWrite', this, [null], { error: err });
  }
}

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes bulkWrite() to call error post hooks when a pre-hook throws an error, ensuring consistency with insertMany() behavior. The fix addresses issue #15881 where error post hooks were not being triggered when pre-hooks failed.

Key changes:

  • Modified error handling in bulkWrite() to call execPost with error context when pre-hooks throw
  • Added comprehensive test coverage for pre-hook error scenarios in both bulkWrite() and insertMany()

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
lib/model.js Updated bulkWrite() error handling to call error post hooks when pre-hooks throw, replacing .catch() chain with try-catch block
test/model.middleware.test.js Added test suite verifying bulkWrite() error propagation, error post hook invocation, and operation prevention when pre-hooks fail
test/model.insertMany.test.js Added parallel test suite for insertMany() to document and verify consistent behavior with bulkWrite()

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

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

LGTM

throw options.preHookError;
});

if (options.postHook) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

createTextContext() is fine, but in this case it feels like you're building a mini-framework for this test suite, which hurts readability and doesn't meaningfully reduce SLOC. Fine for now, but let's avoid creating too many abstractions in the future.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@vkarpov15
My aim is to have the it('...') blocks be as minimal as possible and only showing the important parts to understand what a test does, with building the "common" blocks in createTestContext, which I think you find acceptable.
Do you think instead of having a generic createTestContext, we should try to have multiple createTestContexts specific to each use-case even if they're only slightly different?

@vkarpov15 vkarpov15 merged commit 636c7b6 into master Dec 12, 2025
55 checks passed
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.

[Bug] bulkWrite pre-hook errors do not trigger post error hooks

4 participants