diff options
author | Jonathan Reams <jbreams@mongodb.com> | 2017-02-02 15:41:11 -0500 |
---|---|---|
committer | Jonathan Reams <jbreams@mongodb.com> | 2017-02-09 11:11:38 -0500 |
commit | 1b49f7f0e3d72662ddf81d7c3ed5254d874a0673 (patch) | |
tree | 2cd844dc5ade3e111be9c645edd042a188950d9a /src/mongo/shell | |
parent | 2be5445f1d34ed3708357d23b16fa3d13c34dfa2 (diff) | |
download | mongo-1b49f7f0e3d72662ddf81d7c3ed5254d874a0673.tar.gz |
SERVER-26623 Check whether error is BulkWriteError before unpacking it
Diffstat (limited to 'src/mongo/shell')
-rw-r--r-- | src/mongo/shell/crud_api.js | 103 |
1 files changed, 96 insertions, 7 deletions
diff --git a/src/mongo/shell/crud_api.js b/src/mongo/shell/crud_api.js index 325acbdbaa0..99904fcb3ea 100644 --- a/src/mongo/shell/crud_api.js +++ b/src/mongo/shell/crud_api.js @@ -237,8 +237,22 @@ DBCollection.prototype.insertOne = function(document, options) { var bulk = this.initializeOrderedBulkOp(); bulk.insert(document); - // Execute insert - bulk.execute(writeConcern); + try { + // Execute insert + bulk.execute(writeConcern); + } catch (err) { + if (err instanceof BulkWriteError) { + if (err.hasWriteErrors()) { + throw err.getWriteErrorAt(0); + } + + if (err.hasWriteConcernError()) { + throw err.getWriteConcernError(); + } + } + + throw err; + } if (!result.acknowledged) { return result; @@ -334,7 +348,22 @@ DBCollection.prototype.deleteOne = function(filter, options) { // Add the deleteOne operation. removeOp.removeOne(); - var r = bulk.execute(writeConcern); + try { + // Remove the first document that matches the selector + var r = bulk.execute(writeConcern); + } catch (err) { + if (err instanceof BulkWriteError) { + if (err.hasWriteErrors()) { + throw err.getWriteErrorAt(0); + } + + if (err.hasWriteConcernError()) { + throw err.getWriteConcernError(); + } + } + + throw err; + } if (!result.acknowledged) { return result; @@ -376,7 +405,22 @@ DBCollection.prototype.deleteMany = function(filter, options) { // Add the deleteOne operation. removeOp.remove(); - var r = bulk.execute(writeConcern); + try { + // Remove all documents that matche the selector + var r = bulk.execute(writeConcern); + } catch (err) { + if (err instanceof BulkWriteError) { + if (err.hasWriteErrors()) { + throw err.getWriteErrorAt(0); + } + + if (err.hasWriteConcernError()) { + throw err.getWriteConcernError(); + } + } + + throw err; + } if (!result.acknowledged) { return result; @@ -430,7 +474,22 @@ DBCollection.prototype.replaceOne = function(filter, replacement, options) { op.replaceOne(replacement); - var r = bulk.execute(writeConcern); + try { + // Replace the document + var r = bulk.execute(writeConcern); + } catch (err) { + if (err instanceof BulkWriteError) { + if (err.hasWriteErrors()) { + throw err.getWriteErrorAt(0); + } + + if (err.hasWriteConcernError()) { + throw err.getWriteConcernError(); + } + } + + throw err; + } if (!result.acknowledged) { return result; @@ -494,7 +553,22 @@ DBCollection.prototype.updateOne = function(filter, update, options) { op.updateOne(update); - var r = bulk.execute(writeConcern); + try { + // Update the first document that matches the selector + var r = bulk.execute(writeConcern); + } catch (err) { + if (err instanceof BulkWriteError) { + if (err.hasWriteErrors()) { + throw err.getWriteErrorAt(0); + } + + if (err.hasWriteConcernError()) { + throw err.getWriteConcernError(); + } + } + + throw err; + } if (!result.acknowledged) { return result; @@ -558,7 +632,22 @@ DBCollection.prototype.updateMany = function(filter, update, options) { op.update(update); - var r = bulk.execute(writeConcern); + try { + // Update all documents that match the selector + var r = bulk.execute(writeConcern); + } catch (err) { + if (err instanceof BulkWriteError) { + if (err.hasWriteErrors()) { + throw err.getWriteErrorAt(0); + } + + if (err.hasWriteConcernError()) { + throw err.getWriteConcernError(); + } + } + + throw err; + } if (!result.acknowledged) { return result; |