Skip to content

Commit

Permalink
fix(bulk): handle MongoWriteConcernErrors
Browse files Browse the repository at this point in the history
Fixes NODE-1521
  • Loading branch information
kvwalker committed Jun 25, 2018
1 parent e0be020 commit 12ff392
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 137 deletions.
54 changes: 37 additions & 17 deletions lib/bulk/common.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';

var Long = require('mongodb-core').BSON.Long,
MongoError = require('mongodb-core').MongoError,
util = require('util');
const Long = require('mongodb-core').BSON.Long;
const MongoError = require('mongodb-core').MongoError;
const util = require('util');
const toError = require('../utils').toError;
const handleCallback = require('../utils').handleCallback;

// Error codes
var UNKNOWN_ERROR = 8;
Expand Down Expand Up @@ -402,6 +404,20 @@ var cloneOptions = function(options) {
return clone;
};

function handleMongoWriteConcernError(batch, bulkResult, ordered, err, callback) {
mergeBatchResults(ordered, batch, bulkResult, null, err.result);

const wrappedWriteConcernError = new WriteConcernError({
errmsg: err.result.writeConcernError.errmsg,
code: err.result.writeConcernError.result
});
return handleCallback(
callback,
new BulkWriteError(toError(wrappedWriteConcernError), new BulkWriteResult(bulkResult)),
null
);
}

/**
* Creates a new BulkWriteError
*
Expand All @@ -426,17 +442,21 @@ const BulkWriteError = function(error, result) {
util.inherits(BulkWriteError, MongoError);

// Exports symbols
exports.BulkWriteError = BulkWriteError;
exports.BulkWriteResult = BulkWriteResult;
exports.WriteError = WriteError;
exports.Batch = Batch;
exports.LegacyOp = LegacyOp;
exports.mergeBatchResults = mergeBatchResults;
exports.cloneOptions = cloneOptions;
exports.INVALID_BSON_ERROR = INVALID_BSON_ERROR;
exports.WRITE_CONCERN_ERROR = WRITE_CONCERN_ERROR;
exports.MULTIPLE_ERROR = MULTIPLE_ERROR;
exports.UNKNOWN_ERROR = UNKNOWN_ERROR;
exports.INSERT = INSERT;
exports.UPDATE = UPDATE;
exports.REMOVE = REMOVE;
module.exports = {
Batch,
BulkWriteError,
BulkWriteResult,
cloneOptions,
handleMongoWriteConcernError,
LegacyOp,
mergeBatchResults,
INVALID_BSON_ERROR: INVALID_BSON_ERROR,
MULTIPLE_ERROR: MULTIPLE_ERROR,
UNKNOWN_ERROR: UNKNOWN_ERROR,
WRITE_CONCERN_ERROR: WRITE_CONCERN_ERROR,
INSERT: INSERT,
UPDATE: UPDATE,
REMOVE: REMOVE,
WriteError,
WriteConcernError
};
10 changes: 8 additions & 2 deletions lib/bulk/ordered.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const mergeBatchResults = common.mergeBatchResults;
const executeOperation = utils.executeOperation;
const BulkWriteError = require('./common').BulkWriteError;
const applyWriteConcern = utils.applyWriteConcern;
const MongoWriteConcernError = require('mongodb-core').MongoWriteConcernError;
const handleMongoWriteConcernError = require('./common').handleMongoWriteConcernError;

var bson = new BSON([
BSON.Binary,
Expand Down Expand Up @@ -436,15 +438,19 @@ var executeCommands = function(self, options, callback) {

var resultHandler = function(err, result) {
// Error is a driver related error not a bulk op error, terminate
if ((err && err.driver) || (err && err.message)) {
if (((err && err.driver) || (err && err.message)) && !(err instanceof MongoWriteConcernError)) {
return handleCallback(callback, err);
}

// If we have and error
if (err) err.ok = 0;
if (err instanceof MongoWriteConcernError) {
return handleMongoWriteConcernError(batch, self.s.bulkResult, true, err, callback);
}

// Merge the results together
var mergeResult = mergeBatchResults(true, batch, self.s.bulkResult, err, result);
const writeResult = new BulkWriteResult(self.s.bulkResult);
const mergeResult = mergeBatchResults(true, batch, self.s.bulkResult, err, result);
if (mergeResult != null) {
return handleCallback(callback, null, writeResult);
}
Expand Down
7 changes: 6 additions & 1 deletion lib/bulk/unordered.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const mergeBatchResults = common.mergeBatchResults;
const executeOperation = utils.executeOperation;
const BulkWriteError = require('./common').BulkWriteError;
const applyWriteConcern = utils.applyWriteConcern;
const MongoWriteConcernError = require('mongodb-core').MongoWriteConcernError;
const handleMongoWriteConcernError = require('./common').handleMongoWriteConcernError;

var bson = new BSON([
BSON.Binary,
Expand Down Expand Up @@ -445,12 +447,15 @@ var executeBatch = function(self, batch, options, callback) {

var resultHandler = function(err, result) {
// Error is a driver related error not a bulk op error, terminate
if ((err && err.driver) || (err && err.message)) {
if (((err && err.driver) || (err && err.message)) && !(err instanceof MongoWriteConcernError)) {
return handleCallback(callback, err);
}

// If we have and error
if (err) err.ok = 0;
if (err instanceof MongoWriteConcernError) {
return handleMongoWriteConcernError(batch, self.s.bulkResult, false, err, callback);
}
handleCallback(callback, null, mergeBatchResults(false, batch, self.s.bulkResult, err, result));
};

Expand Down
Loading

0 comments on commit 12ff392

Please sign in to comment.