// 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"]; } }