mirror of
https://github.com/nasa/openmct.git
synced 2025-06-16 14:18:16 +00:00
[Persistence] Reject promises for failed updates
Reject promises for failed update attempts, WTD-1033.
This commit is contained in:
@ -9,7 +9,8 @@ define(
|
|||||||
// so hide them here.
|
// so hide them here.
|
||||||
var SRC = "_source",
|
var SRC = "_source",
|
||||||
REV = "_version",
|
REV = "_version",
|
||||||
ID = "_id";
|
ID = "_id",
|
||||||
|
CONFLICT = 409;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The ElasticPersistenceProvider reads and writes JSON documents
|
* The ElasticPersistenceProvider reads and writes JSON documents
|
||||||
@ -69,15 +70,31 @@ define(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle an update error
|
||||||
|
function handleError(response, key) {
|
||||||
|
var error = new Error("Persistence error.");
|
||||||
|
if ((response || {}).status === CONFLICT) {
|
||||||
|
error.key = "revision";
|
||||||
|
// Load the updated model, then reject the promise
|
||||||
|
return get(key).then(function (model) {
|
||||||
|
error.model = model;
|
||||||
|
throw error;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Reject the promise
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
// Check the response to a create/update/delete request;
|
// Check the response to a create/update/delete request;
|
||||||
// track the rev if it's valid, otherwise return false to
|
// track the rev if it's valid, otherwise return false to
|
||||||
// indicate that the request failed.
|
// indicate that the request failed.
|
||||||
function checkResponse(response) {
|
function checkResponse(response, key) {
|
||||||
if (response && response.ok) {
|
var error;
|
||||||
revs[response.id] = response.rev;
|
if (response && !response.error) {
|
||||||
return response.ok;
|
revs[ID] = response[REV];
|
||||||
|
return response;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return handleError(response, key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,8 +156,11 @@ define(
|
|||||||
* operation
|
* operation
|
||||||
*/
|
*/
|
||||||
updateObject: function (space, key, value) {
|
updateObject: function (space, key, value) {
|
||||||
|
function checkUpdate(response) {
|
||||||
|
return checkResponse(response, key);
|
||||||
|
}
|
||||||
return put(key, value, { version: revs[key] })
|
return put(key, value, { version: revs[key] })
|
||||||
.then(checkResponse);
|
.then(checkUpdate);
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* Delete an object in the specified persistence space.
|
* Delete an object in the specified persistence space.
|
||||||
|
Reference in New Issue
Block a user