Skip to content

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

@AbdelrahmanHafez

Description

@AbdelrahmanHafez

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

9.0.1

Node.js version

N/A

MongoDB server version

N/A

Typescript version (if applicable)

N/A

Description

When a bulkWrite pre-hook throws an error, error post hooks are not called. This is inconsistent with other middleware like save() and insertMany(), which correctly call error post hooks when pre-hooks throw.

Steps to Reproduce

'use strict';

const mongoose = require('mongoose');
const assert = require('assert');

run().catch(err => {
  console.error(err);
  process.exit(1);
});

async function run() {
  await mongoose.connect('mongodb://127.0.0.1:27017/test');
  await mongoose.connection.dropDatabase();

  // Test 1: insertMany correctly calls error post hooks (WORKS)
  {
    const schema = new mongoose.Schema({ name: String });

    let postErrorHookCalled = false;
    schema.pre('insertMany', function() {
      throw new Error('insertMany pre-hook error');
    });
    schema.post('insertMany', function(error, docs, next) {
      postErrorHookCalled = true;
      next(error);
    });

    const Model = mongoose.model('InsertManyTest', schema);

    const err = await Model.insertMany([{ name: 'test' }]).catch(e => e);
    assert.equal(err.message, 'insertMany pre-hook error');
    assert.equal(postErrorHookCalled, true, 'insertMany: error post hook should be called');
    console.log('insertMany: PASS - error post hook was called');
  }

  // Test 2: bulkWrite does NOT call error post hooks (BUG)
  {
    const schema = new mongoose.Schema({ name: String });

    let postErrorHookCalled = false;
    schema.pre('bulkWrite', function() {
      throw new Error('bulkWrite pre-hook error');
    });
    schema.post('bulkWrite', function(error, res, next) {
      postErrorHookCalled = true;
      next(error);
    });

    const Model = mongoose.model('BulkWriteTest', schema);

    const err = await Model.bulkWrite([{ insertOne: { document: { name: 'test' } } }]).catch(e => e);
    assert.equal(err.message, 'bulkWrite pre-hook error');
    assert.equal(postErrorHookCalled, true, 'bulkWrite: error post hook should be called');
    console.log('bulkWrite: PASS - error post hook was called');
  }

  console.log('All tests passed!');
  await mongoose.disconnect();
}

Actual output:

insertMany: PASS - error post hook was called
AssertionError [ERR_ASSERTION]: bulkWrite: error post hook should be called

Expected Behavior

Expected output:

insertMany: PASS - error post hook was called
bulkWrite: PASS - error post hook was called
All tests passed!

Both insertMany and bulkWrite should call error post hooks when pre-hooks throw an error. This is consistent with:

  1. Kareem's wrap() function, which calls execPost with { error } when pre-hooks throw
  2. Document middleware like save(), which calls error post hooks when pre-hooks throw
  3. insertMany(), which correctly calls error post hooks when pre-hooks throw

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions