4xckLZzYr4XSXhbAAGIBhj changeset

Changeset363161623439 (b)
ParentNone (a)
ab
0+// failures maps failed save attempts to the error that caused them (validation, conflict, etc.)
0+function(doc, req, failures) {
0+  var newDoc = doc || {_id: req.query.id || "example"};
0+
0+  if(!(newDoc._id in failures)) {
0+    // Legacy code does this now. It knows nothing about the failures argument, but
0+    // just blindly attempts a save. Possibilities:
0+    // 1. Successful save. Normal code path.
0+    // 2. Conflict or invalid. Erlang has 2 choices.
0+    //    A. newDoc._id is not in failures; call me again with newDoc._id
0+    //       added to the failures object.
0+    //    B. newDoc._id is in failures. Erlang knows my code is naive or does
0+    //       not know the new API; Same 409 Conflict as before.
0+    return [newDoc, "Attempting a smart save: " + newDoc._id + "\n"];
0+  } else {
0+    var reason;
0+    if('validation' in failures[newDoc._id]) {
0+      reason = "Validation failure: " + JSON.stringify(failures[newDoc._id].validation);
0+    } else if ('conflict' in failures[newDoc._id]) {
0+      reason = "Document update conflict";
0+    } else {
0+      reason = 'Unknown reason';
0+    }
0+    return [null, "Sorry, this is not possible: " + reason + "\n"];
0+  }
0+}
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
--- Revision None
+++ Revision 363161623439
@@ -0,0 +1,26 @@
+// failures maps failed save attempts to the error that caused them (validation, conflict, etc.)
+function(doc, req, failures) {
+ var newDoc = doc || {_id: req.query.id || "example"};
+
+ if(!(newDoc._id in failures)) {
+ // Legacy code does this now. It knows nothing about the failures argument, but
+ // just blindly attempts a save. Possibilities:
+ // 1. Successful save. Normal code path.
+ // 2. Conflict or invalid. Erlang has 2 choices.
+ // A. newDoc._id is not in failures; call me again with newDoc._id
+ // added to the failures object.
+ // B. newDoc._id is in failures. Erlang knows my code is naive or does
+ // not know the new API; Same 409 Conflict as before.
+ return [newDoc, "Attempting a smart save: " + newDoc._id + "\n"];
+ } else {
+ var reason;
+ if('validation' in failures[newDoc._id]) {
+ reason = "Validation failure: " + JSON.stringify(failures[newDoc._id].validation);
+ } else if ('conflict' in failures[newDoc._id]) {
+ reason = "Document update conflict";
+ } else {
+ reason = 'Unknown reason';
+ }
+ return [null, "Sorry, this is not possible: " + reason + "\n"];
+ }
+}