diff --git a/platform/core/bundle.json b/platform/core/bundle.json index b22e225e84..0a76948e39 100644 --- a/platform/core/bundle.json +++ b/platform/core/bundle.json @@ -188,7 +188,8 @@ { "key": "persistence", "implementation": "capabilities/PersistenceCapability.js", - "depends": [ "persistenceService", "identifierService" ] + "depends": [ "persistenceService", "identifierService", + "notificationService", "$q" ] }, { "key": "metadata", diff --git a/platform/core/src/capabilities/PersistenceCapability.js b/platform/core/src/capabilities/PersistenceCapability.js index 8bd29c7b7c..52f2787f51 100644 --- a/platform/core/src/capabilities/PersistenceCapability.js +++ b/platform/core/src/capabilities/PersistenceCapability.js @@ -47,6 +47,8 @@ define( function PersistenceCapability( persistenceService, identifierService, + alertService, + $q, domainObject ) { // Cache modified timestamp @@ -55,6 +57,8 @@ define( this.domainObject = domainObject; this.identifierService = identifierService; this.persistenceService = persistenceService; + this.alertService = alertService; + this.$q = $q; } // Utility function for creating promise-like objects which @@ -72,6 +76,34 @@ define( 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 * domain object's model. @@ -80,7 +112,8 @@ define( * if not. */ PersistenceCapability.prototype.persist = function () { - var domainObject = this.domainObject, + var self = this, + domainObject = this.domainObject, model = domainObject.getModel(), modified = model.modified, persistenceService = this.persistenceService, @@ -98,7 +131,11 @@ define( this.getSpace(), getKey(domainObject.getId()), domainObject.getModel() - ]); + ]).then(function(result){ + return rejectIfFalsey(result, self.$q); + }).catch(function(error){ + return notifyOnError(error, domainObject, self.alertService, self.$q); + }); }; /**