From c30b7b0d8030e6b91f102f12df160c63a72b76c5 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Thu, 26 Apr 2018 21:11:51 -0700 Subject: [PATCH] GUACAMOLE-526: Add convenience function for generating promise callbacks which handle only REST errors. --- .../app/rest/services/requestService.js | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/guacamole/src/main/webapp/app/rest/services/requestService.js b/guacamole/src/main/webapp/app/rest/services/requestService.js index f17ce251a..b2f709b14 100644 --- a/guacamole/src/main/webapp/app/rest/services/requestService.js +++ b/guacamole/src/main/webapp/app/rest/services/requestService.js @@ -26,6 +26,7 @@ angular.module('rest').factory('requestService', ['$injector', // Required services var $http = $injector.get('$http'); + var $log = $injector.get('$log'); // Required types var Error = $injector.get('Error'); @@ -62,6 +63,34 @@ angular.module('rest').factory('requestService', ['$injector', ); }; - return service; + /** + * Creates a promise error callback which invokes the given callback only + * if the promise was rejected with a REST @link{Error} object. If the + * promise is rejected without an @link{Error} object, such as when a + * JavaScript error occurs within a callback earlier in the promise chain, + * the rejection is logged without invoking the given callback. + * + * @param {Function} callback + * The callback to invoke if the promise is rejected with an + * @link{Error} object. + * + * @returns {Function} + * A function which can be provided as the error callback for a + * promise. + */ + service.createErrorCallback = function createErrorCallback(callback) { + return (function generatedErrorCallback(error) { + + // Invoke given callback ONLY if due to a legitimate REST error + if (error instanceof Error) + return callback(error); + + // Log all other errors + $log.error(error); + + }); + }; + + return service; }]);