From b539608330c2b8f4250272269cff4b97d04f3891 Mon Sep 17 00:00:00 2001 From: Alec Gibson <12036746+alecgibson@users.noreply.github.com> Date: Wed, 29 May 2024 08:28:26 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20error=20handling=20in=20`?= =?UTF-8?q?=5FfetchCreateOpVersion()`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We're currently not returning errors from the database adapter if there are issues fetching the committed op, because of a code typo. This change adds coverage for this case and fixes the bug. --- lib/submit-request.js | 2 +- test/client/submit.js | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/submit-request.js b/lib/submit-request.js index 5c33fb9b1..ff379801c 100644 --- a/lib/submit-request.js +++ b/lib/submit-request.js @@ -110,7 +110,7 @@ SubmitRequest.prototype.submit = function(callback) { // must get the past ops and check their src and seq values to // differentiate. request._fetchCreateOpVersion(function(error, version) { - if (err) return callback(err); + if (error) return callback(error); if (version == null) { callback(request.alreadyCreatedError()); } else { diff --git a/test/client/submit.js b/test/client/submit.js index 8f71e00ec..aa3f6850b 100644 --- a/test/client/submit.js +++ b/test/client/submit.js @@ -6,7 +6,6 @@ var deserializedType = require('./deserialized-type'); var numberType = require('./number-type'); var errorHandler = require('../util').errorHandler; var richText = require('rich-text'); -var MemoryDB = require('../../lib/db/memory'); types.register(deserializedType.type); types.register(deserializedType.type2); types.register(numberType.type); @@ -215,8 +214,8 @@ module.exports = function() { describe('no snapshot metadata available', function() { beforeEach(function() { - var getSnapshot = MemoryDB.prototype.getSnapshot; - sinon.stub(MemoryDB.prototype, 'getSnapshot') + var getSnapshot = this.backend.db.getSnapshot; + sinon.stub(this.backend.db, 'getSnapshot') .callsFake(function() { var args = Array.from(arguments); var callback = args.pop(); @@ -233,6 +232,27 @@ module.exports = function() { }); runCreateTests(); + + it('returns errors if the database cannot get committed op version', function(done) { + sinon.stub(this.backend.db, 'getCommittedOpVersion') + .callsFake(function() { + var args = Array.from(arguments); + var callback = args.pop(); + callback(new Error('uh-oh')); + }); + + var doc1 = this.backend.connect().get('dogs', 'fido'); + var doc2 = this.backend.connect().get('dogs', 'fido'); + async.series([ + doc1.create.bind(doc1, {age: 3}), + function(next) { + doc2.create({name: 'Fido'}, function(error) { + expect(error.message).to.equal('uh-oh'); + next(); + }); + } + ], done); + }); }); function runCreateTests() {