You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@guacamole.apache.org by vn...@apache.org on 2018/04/29 20:58:52 UTC

[10/19] guacamole-client git commit: GUACAMOLE-526: Add convenience function for generating promise callbacks which handle only REST errors.

GUACAMOLE-526: Add convenience function for generating promise callbacks which handle only REST errors.

Project: http://git-wip-us.apache.org/repos/asf/guacamole-client/repo
Commit: http://git-wip-us.apache.org/repos/asf/guacamole-client/commit/c30b7b0d
Tree: http://git-wip-us.apache.org/repos/asf/guacamole-client/tree/c30b7b0d
Diff: http://git-wip-us.apache.org/repos/asf/guacamole-client/diff/c30b7b0d

Branch: refs/heads/master
Commit: c30b7b0d8030e6b91f102f12df160c63a72b76c5
Parents: 8c9735d
Author: Michael Jumper <mj...@apache.org>
Authored: Thu Apr 26 21:11:51 2018 -0700
Committer: Michael Jumper <mj...@apache.org>
Committed: Thu Apr 26 21:11:51 2018 -0700

----------------------------------------------------------------------
 .../webapp/app/rest/services/requestService.js  | 31 +++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/guacamole-client/blob/c30b7b0d/guacamole/src/main/webapp/app/rest/services/requestService.js
----------------------------------------------------------------------
diff --git a/guacamole/src/main/webapp/app/rest/services/requestService.js b/guacamole/src/main/webapp/app/rest/services/requestService.js
index f17ce25..b2f709b 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;
 
 }]);