Skip to content

Commit

Permalink
Merge pull request #506 from share/normalize-error
Browse files Browse the repository at this point in the history
🥅 Catch `json0` normalization errors
  • Loading branch information
alecgibson authored Aug 10, 2021
2 parents 6d9afcb + e199a68 commit 99ea28e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ ShareDBError.CODES = {
ERR_OP_VERSION_MISMATCH_AFTER_TRANSFORM: 'ERR_OP_VERSION_MISMATCH_AFTER_TRANSFORM',
ERR_OP_VERSION_MISMATCH_DURING_TRANSFORM: 'ERR_OP_VERSION_MISMATCH_DURING_TRANSFORM',
ERR_OP_VERSION_NEWER_THAN_CURRENT_SNAPSHOT: 'ERR_OP_VERSION_NEWER_THAN_CURRENT_SNAPSHOT',
ERR_OT_LEGACY_JSON0_OP_CANNOT_BE_NORMALIZED: 'ERR_OT_LEGACY_JSON0_OP_CANNOT_BE_NORMALIZED',
ERR_OT_OP_BADLY_FORMED: 'ERR_OT_OP_BADLY_FORMED',
ERR_OT_OP_NOT_APPLIED: 'ERR_OT_OP_NOT_APPLIED',
ERR_OT_OP_NOT_PROVIDED: 'ERR_OT_OP_NOT_PROVIDED',
Expand Down
11 changes: 10 additions & 1 deletion lib/ot.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,16 @@ exports.applyOps = function(snapshot, ops, options) {
options = options || {};
for (var index = 0; index < ops.length; index++) {
var op = ops[index];
if (options._normalizeLegacyJson0Ops) normalizeLegacyJson0Ops(snapshot, op);
if (options._normalizeLegacyJson0Ops) {
try {
normalizeLegacyJson0Ops(snapshot, op);
} catch (error) {
return new ShareDBError(
ERROR_CODE.ERR_OT_LEGACY_JSON0_OP_CANNOT_BE_NORMALIZED,
'Cannot normalize legacy json0 op'
);
}
}
snapshot.v = op.v;
var error = exports.apply(snapshot, op);
if (error) return error;
Expand Down

0 comments on commit 99ea28e

Please sign in to comment.