Revision 363161623439 () - Diff

Link to this snippet: https://friendpaste.com/4xckLZzYr4XSXhbAAGIBhj
Embed:
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
// 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"];
}
}