summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorByron Ruth <bruth@codeomics.com>2011-09-16 22:17:09 -0400
committerByron Ruth <bruth@codeomics.com>2011-09-16 22:17:09 -0400
commit34886739b16e5fa7f3469f93cffd7b2dc40d27bb (patch)
tree8c410858c2c45d56f7c0c1c54e6f5e98cdcc78a4
parent033c5f6ac9778c6c968578cc348889eb58d30be8 (diff)
downloadpython-json-patch-34886739b16e5fa7f3469f93cffd7b2dc40d27bb.tar.gz
Add JsonPatchConflict for patch conflicts
This is necessary in order to differentiate between general processing errors e.g. bad operation and conflicts between the patch and the state of the data the patch is being applied to.
-rw-r--r--jsonpatch.py18
1 files changed, 11 insertions, 7 deletions
diff --git a/jsonpatch.py b/jsonpatch.py
index d75e27c..7ada8e3 100644
--- a/jsonpatch.py
+++ b/jsonpatch.py
@@ -47,6 +47,10 @@ class JsonPatchException(Exception):
pass
+class JsonPatchConflict(JsonPatchException):
+ pass
+
+
def apply_patch(doc, patch):
"""
>>> obj = { 'baz': 'qux', 'foo': 'bar' }
@@ -139,7 +143,7 @@ class PatchOperation(object):
continue
if must_exist:
- raise JsonPatchException('key %s not found' % loc_part)
+ raise JsonPatchConflict('key %s not found' % loc_part)
else:
return obj, part_variants[0]
@@ -191,18 +195,18 @@ class AddOperation(PatchOperation):
if isinstance(subobj, list):
if part > len(subobj) or part < 0:
- raise JsonPatchException("can't insert outside of list")
+ raise JsonPatchConflict("can't insert outside of list")
subobj.insert(part, value)
elif isinstance(subobj, dict):
if part in subobj:
- raise JsonPatchException("object '%s' already exists" % part)
+ raise JsonPatchConflict("object '%s' already exists" % part)
subobj[part] = value
else:
- raise JsonPatchException("can't add to type '%s'" % subobj.__class__.__name__)
+ raise JsonPatchConflict("can't add to type '%s'" % subobj.__class__.__name__)
class ReplaceOperation(PatchOperation):
@@ -221,13 +225,13 @@ class ReplaceOperation(PatchOperation):
if isinstance(subobj, list):
if part > len(subobj) or part < 0:
- raise JsonPatchException("can't replace outside of list")
+ raise JsonPatchConflict("can't replace outside of list")
elif isinstance(subobj, dict):
if not part in subobj:
- raise JsonPatchException("can't replace non-existant object '%s'" % part)
+ raise JsonPatchConflict("can't replace non-existant object '%s'" % part)
else:
- raise JsonPatchException("can't replace in type '%s'" % subobj.__class__.__name__)
+ raise JsonPatchConflict("can't replace in type '%s'" % subobj.__class__.__name__)
subobj[part] = value