| a | b | |
|---|
| 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 | + | } |
|---|
| ... | |
|---|