diff --git a/docs/api/sharedb-error.md b/docs/api/sharedb-error.md index a0564b714..4dc89fcd0 100644 --- a/docs/api/sharedb-error.md +++ b/docs/api/sharedb-error.md @@ -35,7 +35,7 @@ Representation of an error, with a machine-parsable [code](#error-codes). > This error might be used as part of standard control flow. For example, consumers may define a middleware that validates document structure, and rejects operations that do not conform to this schema using this error code to reset the client to a valid state. -### `ERR_OP_PENDING_OP_SUBMIT_REJECTED` +### `ERR_PENDING_OP_REMOVED_BY_OP_SUBMIT_REJECTED` > This may happen if server rejected op with ERR_OP_SUBMIT_REJECTED and the type is not invertible or there are some pending ops after the create op was rejected with ERR_OP_SUBMIT_REJECTED diff --git a/lib/client/doc.js b/lib/client/doc.js index b3f4a78b8..14c25bb4b 100644 --- a/lib/client/doc.js +++ b/lib/client/doc.js @@ -1051,39 +1051,32 @@ Doc.prototype._hardRollback = function(err) { doc.emit('error', fetchError); } - if (err.code !== ERROR_CODE.ERR_OP_SUBMIT_REJECTED) { - if (inflightOp) pendingOps.push(inflightOp); - var allOpsHadCallbacks = !!pendingOps.length; - for (var i = 0; i < pendingOps.length; i++) { - allOpsHadCallbacks = util.callEach(pendingOps[i].callbacks, err) && allOpsHadCallbacks; + if (err.code === ERROR_CODE.ERR_OP_SUBMIT_REJECTED) { + /** + * Handle special case of ERR_OP_SUBMIT_REJECTED + * This ensures that we resolve the main op callback and reject + * all the pending ops. This is hard rollback so all the pending ops will be + * discarded. This will ensure that the user is at least informed about it. + * more info: https://github.com/share/sharedb/pull/626 + */ + if (inflightOp) { + util.callEach(inflightOp.callbacks); + inflightOp = null; } - if (err && !allOpsHadCallbacks) doc.emit('error', err); - return; - } - /** - * Handle special case of ERR_OP_SUBMIT_REJECTED - * This ensures that we resolve the main op callback and reject - * all the pending ops. This is hard rollback so all the pending ops will be - * discarded. This will ensure that the user is at least informed about it. - * more info: https://github.com/share/sharedb/pull/626 - */ - if (inflightOp) { - util.callEach(inflightOp.callbacks); + if (!pendingOps.length) return; + err = new ShareDBError( + ERROR_CODE.ERR_PENDING_OP_REMOVED_BY_OP_SUBMIT_REJECTED, + 'Pending op are present when doing rollback of invertible operation' + ); } - if (!pendingOps.length) return; - - var hardRollbackError = new ShareDBError( - ERROR_CODE.ERR_PENDING_OP_REMOVED_BY_OP_SUBMIT_REJECTED, - 'ending op removed by hard rollback caused by ERR_OP_SUBMIT_REJECTED' - ); - + if (inflightOp) pendingOps.push(inflightOp); var allOpsHadCallbacks = !!pendingOps.length; for (var i = 0; i < pendingOps.length; i++) { - allOpsHadCallbacks = util.callEach(pendingOps[i].callbacks, hardRollbackError) && allOpsHadCallbacks; + allOpsHadCallbacks = util.callEach(pendingOps[i].callbacks, err) && allOpsHadCallbacks; } - if (!allOpsHadCallbacks) return doc.emit('error', hardRollbackError); + if (err && !allOpsHadCallbacks) doc.emit('error', err); }); };