[Persistence] Errors in persistence (after creating/modifying objects) should be visible to user #58

This commit is contained in:
Henry 2015-12-01 21:58:57 -08:00
parent 3fd4304de1
commit 26cf9c14f4
2 changed files with 41 additions and 3 deletions

View File

@ -188,7 +188,8 @@
{ {
"key": "persistence", "key": "persistence",
"implementation": "capabilities/PersistenceCapability.js", "implementation": "capabilities/PersistenceCapability.js",
"depends": [ "persistenceService", "identifierService" ] "depends": [ "persistenceService", "identifierService",
"notificationService", "$q" ]
}, },
{ {
"key": "metadata", "key": "metadata",

View File

@ -47,6 +47,8 @@ define(
function PersistenceCapability( function PersistenceCapability(
persistenceService, persistenceService,
identifierService, identifierService,
alertService,
$q,
domainObject domainObject
) { ) {
// Cache modified timestamp // Cache modified timestamp
@ -55,6 +57,8 @@ define(
this.domainObject = domainObject; this.domainObject = domainObject;
this.identifierService = identifierService; this.identifierService = identifierService;
this.persistenceService = persistenceService; this.persistenceService = persistenceService;
this.alertService = alertService;
this.$q = $q;
} }
// Utility function for creating promise-like objects which // Utility function for creating promise-like objects which
@ -72,6 +76,34 @@ define(
return parts.length > 1 ? parts.slice(1).join(":") : id; return parts.length > 1 ? parts.slice(1).join(":") : id;
} }
/**
* Checks if the value returned is falsey, and if so returns a
* rejected promise
*/
function rejectIfFalsey(value, $q){
if (!value){
return $q.reject("Error persisting object")
} else {
return value;
}
}
/**
* Display a notification message if an error has occurred during
* persistence.
*/
function notifyOnError(error, domainObject, notificationService, $q){
var errorMessage = "Unable to persist " + domainObject.model.name + ": ";
errorMessage += typeof error === "string" ? error : error.message;
notificationService.error({
title: "Error persisting " + domainObject.model.name,
hint: errorMessage || "Unknown error"
});
return $q.reject(error);
}
/** /**
* Persist any changes which have been made to this * Persist any changes which have been made to this
* domain object's model. * domain object's model.
@ -80,7 +112,8 @@ define(
* if not. * if not.
*/ */
PersistenceCapability.prototype.persist = function () { PersistenceCapability.prototype.persist = function () {
var domainObject = this.domainObject, var self = this,
domainObject = this.domainObject,
model = domainObject.getModel(), model = domainObject.getModel(),
modified = model.modified, modified = model.modified,
persistenceService = this.persistenceService, persistenceService = this.persistenceService,
@ -98,7 +131,11 @@ define(
this.getSpace(), this.getSpace(),
getKey(domainObject.getId()), getKey(domainObject.getId()),
domainObject.getModel() domainObject.getModel()
]); ]).then(function(result){
return rejectIfFalsey(result, self.$q);
}).catch(function(error){
return notifyOnError(error, domainObject, self.alertService, self.$q);
});
}; };
/** /**