usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From snoopd...@apache.org
Subject [21/41] git commit: Test cases for Ajax, Usergrid.Request and Usergrid.Response
Date Tue, 11 Feb 2014 23:21:05 GMT
Test cases for Ajax, Usergrid.Request and Usergrid.Response


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/650cfd5c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/650cfd5c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/650cfd5c

Branch: refs/heads/master
Commit: 650cfd5c03033546a07e4935cedec499ef7bf886
Parents: 017cf30
Author: ryan bridges <rbridges@apigee.com>
Authored: Mon Feb 10 16:34:19 2014 -0500
Committer: ryan bridges <rbridges@apigee.com>
Committed: Mon Feb 10 16:34:19 2014 -0500

----------------------------------------------------------------------
 sdks/html5-javascript/lib/Usergrid.js          |   7 +-
 sdks/html5-javascript/lib/modules/Asset.js     |  10 +-
 sdks/html5-javascript/lib/modules/Client.js    | 115 ++------------------
 sdks/html5-javascript/lib/modules/Entity.js    |   5 +-
 sdks/html5-javascript/lib/modules/Folder.js    |   6 +-
 sdks/html5-javascript/lib/modules/Group.js     |   2 +-
 sdks/html5-javascript/lib/modules/util/Ajax.js | 114 ++++++++++---------
 sdks/html5-javascript/tests/mocha/test.js      |  70 ++++++++++++
 sdks/html5-javascript/usergrid.js              |  61 ++++++-----
 sdks/html5-javascript/usergrid.min.js          |   4 +-
 10 files changed, 185 insertions(+), 209 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/650cfd5c/sdks/html5-javascript/lib/Usergrid.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/Usergrid.js b/sdks/html5-javascript/lib/Usergrid.js
index f545964..e607075 100644
--- a/sdks/html5-javascript/lib/Usergrid.js
+++ b/sdks/html5-javascript/lib/Usergrid.js
@@ -133,19 +133,18 @@ function isFunction(f) {
  *  @return Returns whatever would be returned by the callback. or false.
  */
 function doCallback(callback, params, context) {
-    console.info("CALLED FROM", this.name||(context)?context.name:"UNKNOWN");
 	var returnValue;
 	if (isFunction(callback)) {
 		if (!params) params = [];
 		if (!context) context = this;
 		params.push(context);
-		//try {
+		try {
 			returnValue = callback.apply(context, params);
-		/*} catch (ex) {
+		} catch (ex) {
 			if (console && console.error) {
 				console.error("Callback error:", ex);
 			}
-		}*/
+		}
 	}
 	return returnValue;
 }

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/650cfd5c/sdks/html5-javascript/lib/modules/Asset.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/modules/Asset.js b/sdks/html5-javascript/lib/modules/Asset.js
index 8af84e1..f40618e 100644
--- a/sdks/html5-javascript/lib/modules/Asset.js
+++ b/sdks/html5-javascript/lib/modules/Asset.js
@@ -79,7 +79,7 @@ Usergrid.Asset.prototype.addToFolder = function(options, callback) {
 			this._client.request(options, callback);
 		});
 	} else {
-		doCallback(callback, [true, new Usergrid.Error('folder not specified')], self);
+		doCallback(callback, [true, new UsergridError('folder not specified')], self);
 	}
 };
 
@@ -93,7 +93,7 @@ Usergrid.Asset.prototype.addToFolder = function(options, callback) {
  */
 Usergrid.Asset.prototype.upload = function(data, callback) {
 	if (!(window.File && window.FileReader && window.FileList && window.Blob))
{
-		return doCallback(callback, [true, new Usergrid.Error('The File APIs are not fully supported
by your browser.')], self);
+		return doCallback(callback, [true, new UsergridError('The File APIs are not fully supported
by your browser.')], self);
 	}
 	var self = this;
 	var endpoint = [this._client.URI, this._client.orgName, this._client.appName, "assets",
self.get("uuid"), 'data'].join('/'); //self._client.buildAssetURL(self.get("uuid"));
@@ -102,11 +102,11 @@ Usergrid.Asset.prototype.upload = function(data, callback) {
 	xhr.open("POST", endpoint, true);
 	xhr.onerror = function(err) {
 		//callback(true, err);
-		doCallback(callback, [true, new Usergrid.Error('The File APIs are not fully supported by
your browser.')], self)
+		doCallback(callback, [true, new UsergridError('The File APIs are not fully supported by
your browser.')], self)
 	};
 	xhr.onload = function(ev) {
 		if (xhr.status >= 300) {
-			doCallback(callback, [true, new Usergrid.Error(JSON.parse(xhr.responseText))], self)
+			doCallback(callback, [true, new UsergridError(JSON.parse(xhr.responseText))], self)
 		} else {
 			doCallback(callback, [null, self], self)
 		}
@@ -142,7 +142,7 @@ Usergrid.Asset.prototype.download = function(callback) {
 	};
 	xhr.onerror = function(err) {
 		callback(true, err);
-		doCallback(callback, [true, new Usergrid.Error(err)], self)
+		doCallback(callback, [true, new UsergridError(err)], self)
 	};
 
 	xhr.send();

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/650cfd5c/sdks/html5-javascript/lib/modules/Client.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/modules/Client.js b/sdks/html5-javascript/lib/modules/Client.js
index cf78df7..88ff623 100644
--- a/sdks/html5-javascript/lib/modules/Client.js
+++ b/sdks/html5-javascript/lib/modules/Client.js
@@ -49,10 +49,13 @@
     var orgName = this.get('orgName');
     var appName = this.get('appName');
     var uri;
+      var logoutCallback=function(){
+          if (typeof(this.logoutCallback) === 'function') {
+              return this.logoutCallback(true, 'no_org_or_app_name_specified');
+          }
+      }.bind(this);
     if(!mQuery && !orgName && !appName){
-      if (typeof(this.logoutCallback) === 'function') {
-        return this.logoutCallback(true, 'no_org_or_app_name_specified');
-      }
+        return logoutCallback();
     }
     if (mQuery) {
       uri = this.URI + '/' + endpoint;
@@ -76,115 +79,11 @@
               "unauthorized",
               "auth_invalid"
           ].indexOf(response.error) !== -1) {
-              //throw err;
+              return logoutCallback();
           }
           doCallback(callback, [err, response]);
           //p.done(err, response);
       });
-    /*//append params to the path
-    var encoded_params = encodeParams(qs);
-    if (encoded_params) {
-      uri += "?" + encoded_params;
-    }
-
-    //stringify the body object
-    body = JSON.stringify(body);
-
-    //so far so good, so run the query
-    var xhr = new XMLHttpRequest();
-    xhr.open(method, uri, true);
-    //add content type = json if there is a json payload
-    if (body) {
-      xhr.setRequestHeader("Content-Type", "application/json");
-      xhr.setRequestHeader("Accept", "application/json");
-    }
-
-    // Handle response.
-    xhr.onerror = function(response) {
-      self._end = new Date().getTime();
-      if (self.logging) {
-        console.log('success (time: ' + self.calcTimeDiff() + '): ' + method + ' ' + uri);
-      }
-      if (self.logging) {
-        console.log('Error: API call failed at the network level.');
-      }
-      //network error
-      clearTimeout(timeout);
-      var err = true;
-      if (typeof(callback) === 'function') {
-        callback(err, response);
-      }
-    };
-
-    xhr.onload = function(response) {
-      //call timing, get time, then log the call
-      self._end = new Date().getTime();
-      if (self.logging) {
-        console.log('success (time: ' + self.calcTimeDiff() + '): ' + method + ' ' + uri);
-      }
-      //call completed
-      clearTimeout(timeout);
-      //decode the response
-      try{
-        response = JSON.parse(xhr.responseText);
-      }catch (e){
-        response = {error:'unhandled_error',error_description:xhr.responseText};
-        xhr.status = xhr.status === 200 ? 400 : xhr.status;
-        console.error(e);
-      }
-      if (xhr.status != 200)   {
-        //there was an api error
-        var error = response.error;
-        var error_description = response.error_description;
-        if (self.logging) {
-          console.log('Error (' + xhr.status + ')(' + error + '): ' + error_description);
-        }
-        if ( (error == "auth_expired_session_token") ||
-          (error == "auth_missing_credentials")   ||
-          (error == "auth_unverified_oath")       ||
-          (error == "expired_token")              ||
-          (error == "unauthorized")               ||
-          (error == "auth_invalid")) {
-          //these errors mean the user is not authorized for whatever reason. If a logout
function is defined, call it
-          //if the user has specified a logout callback:
-          if (typeof(self.logoutCallback) === 'function') {
-            return self.logoutCallback(true, response);
-          }
-        }
-        if (typeof(callback) === 'function') {
-          callback(true, response);
-        }
-      } else {
-        if (typeof(callback) === 'function') {
-          callback(false, response);
-        }
-      }
-    };
-
-    var timeout = setTimeout(
-      function() {
-        xhr.abort();
-        if (self._callTimeoutCallback === 'function') {
-          self._callTimeoutCallback('API CALL TIMEOUT');
-        } else {
-          self.callback('API CALL TIMEOUT');
-        }
-      },
-      self._callTimeout); //set for 30 seconds
-
-    if (this.logging) {
-      console.log('calling: ' + method + ' ' + uri);
-    }
-    if (this.buildCurl) {
-      var curlOptions = {
-        uri:uri,
-        body:body,
-        method:method
-      }
-      this.buildCurlCall(curlOptions);
-    }
-    this._start = new Date().getTime();
-    xhr.send(body);*/
   }
 
   /*

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/650cfd5c/sdks/html5-javascript/lib/modules/Entity.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/modules/Entity.js b/sdks/html5-javascript/lib/modules/Entity.js
index 05ea970..b1225a3 100644
--- a/sdks/html5-javascript/lib/modules/Entity.js
+++ b/sdks/html5-javascript/lib/modules/Entity.js
@@ -1,3 +1,5 @@
+var ENTITY_SYSTEM_PROPERTIES=['metadata','created','modified','oldpassword','newpassword','type','activated','uuid'];
+
 /*
  *  A class to Model a Usergrid Entity.
  *  Set the type and uuid of entity in the 'data' json object
@@ -140,7 +142,6 @@ Usergrid.Entity.prototype.save = function (callback) {
     /*password = this.get('password'),
     oldpassword = this.get('oldpassword'),
     newpassword = this.get('newpassword'),*/
-    SYSTEM_PROPERTIES=['metadata','created','modified','oldpassword','newpassword','type','activated','uuid'],
     options={
       method:method,
       endpoint:type
@@ -155,7 +156,7 @@ Usergrid.Entity.prototype.save = function (callback) {
 
   //remove system-specific properties
   Object.keys(entityData)
-      .filter(function(key){return (SYSTEM_PROPERTIES.indexOf(key)===-1)})
+      .filter(function(key){return (ENTITY_SYSTEM_PROPERTIES.indexOf(key)===-1)})
     .forEach(function(key){
       data[key]= entityData[key];
     });

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/650cfd5c/sdks/html5-javascript/lib/modules/Folder.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/modules/Folder.js b/sdks/html5-javascript/lib/modules/Folder.js
index 7e2821f..d57d4b9 100644
--- a/sdks/html5-javascript/lib/modules/Folder.js
+++ b/sdks/html5-javascript/lib/modules/Folder.js
@@ -49,13 +49,13 @@ Usergrid.Folder.prototype.fetch = function(callback) {
 		if (!err) {
 			self.getAssets(function(err, data) {
 				if (err) {
-					doCallback(callback, [true, new Usergrid.Error(data)], self);
+					doCallback(callback, [true, new UsergridError(data)], self);
 				} else {
 					doCallback(callback, [null, self], self);
 				}
 			});
 		} else {
-			doCallback(callback, [true, new Usergrid.Error(data)], self)
+			doCallback(callback, [true, new UsergridError(data)], self)
 		}
 	})
 };
@@ -93,7 +93,7 @@ Usergrid.Folder.prototype.addAsset = function(options, callback) {
 		if (asset && asset instanceof Usergrid.Entity) {
 			asset.fetch(function(err, data) {
 				if (err) {
-					doCallback(callback, [err, new Usergrid.Error(data)], self)
+					doCallback(callback, [err, new UsergridError(data)], self)
 				} else {
 					var endpoint = ["folders", self.get("uuid"), "assets", asset.get("uuid")].join('/');
 					var options = {

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/650cfd5c/sdks/html5-javascript/lib/modules/Group.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/modules/Group.js b/sdks/html5-javascript/lib/modules/Group.js
index 2a853be..7ae183c 100644
--- a/sdks/html5-javascript/lib/modules/Group.js
+++ b/sdks/html5-javascript/lib/modules/Group.js
@@ -52,7 +52,7 @@ Usergrid.Group.prototype.fetch = function(callback) {
         callback(err, data);
       }
     } else {
-      if(data.entities) {
+      if(data.entities && data.entities.length) {
         var groupData = data.entities[0];
         self._data = groupData || {};
         self._client.request(memberOptions, function(err, data) {

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/650cfd5c/sdks/html5-javascript/lib/modules/util/Ajax.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/lib/modules/util/Ajax.js b/sdks/html5-javascript/lib/modules/util/Ajax.js
index 5b36ad5..a9cf16c 100644
--- a/sdks/html5-javascript/lib/modules/util/Ajax.js
+++ b/sdks/html5-javascript/lib/modules/util/Ajax.js
@@ -2,69 +2,67 @@
 (function() {
     var name = 'Ajax', global = this, overwrittenName = global[name], exports;
 
-    /*Function.prototype.partial = function() {
-        var fn = this,b = [].slice.call(arguments);
-        return fn.bind(undefined, b);
-    }*/
-    function partial(fn){
-        var args = Array.prototype.slice.call(arguments,1);
-        return function(){
-            return fn.apply(this, args.concat(Array.prototype.slice(arguments,0)))
-        }
+    function partial(){
+        var args = Array.prototype.slice.call(arguments);
+        var fn=args.shift();
+        return fn.bind(this, args)
     }
-        function Ajax() {
-            this.logger=new global.Logger(name);
-            var self=this;
-            function encode(data) {
-                var result = "";
-                if (typeof data === "string") {
-                    result = data;
-                } else {
-                    var e = encodeURIComponent;
-                    for (var i in data) {
-                        if (data.hasOwnProperty(i)) {
-                            result += '&' + e(i) + '=' + e(data[i]);
-                        }
+    function Ajax() {
+        this.logger=new global.Logger(name);
+        var self=this;
+        function encode(data) {
+            var result = "";
+            if (typeof data === "string") {
+                result = data;
+            } else {
+                var e = encodeURIComponent;
+                for (var i in data) {
+                    if (data.hasOwnProperty(i)) {
+                        result += '&' + e(i) + '=' + e(data[i]);
                     }
                 }
-                return result;
             }
-            function request(m, u, d) {
-                var p = new Promise(), timeout;
-                self.logger.time(m + ' ' + u);
-                self.logger.timeEnd(m + ' ' + u);
-                (function(xhr) {
-                    xhr.onreadystatechange = function() {
-                        this.readyState ^ 4 || (self.logger.timeEnd(m + ' ' + u), clearTimeout(timeout),
p.done(null, this));
-                    };
-                    xhr.onerror=function(response){
-                        clearTimeout(timeout);
-                        p.done(response, null);
-                    }
-                    xhr.oncomplete=function(response){
-                        clearTimeout(timeout);
-                        self.info("%s request to %s returned %s", m, u, this.status );
-                    }
-                    xhr.open(m, u);
-                    if (d) {
-                        xhr.setRequestHeader("Content-Type", "application/json");
-                        xhr.setRequestHeader("Accept", "application/json");
-                    }
-                    timeout = setTimeout(function() {
-                        xhr.abort();
-                        p.done("API Call timed out.", null)
-                    }, 30000);
-                    //TODO stick that timeout in a config variable
-                    xhr.send(encode(d));
-                }(new XMLHttpRequest()));
-                return p;
-            };
-            this.request=request;
-            this.get = partial(request, 'GET');
-            this.post = partial(request, 'POST');
-            this.put = partial(request, 'PUT');
-            this.delete = partial(request, 'DELETE');
+            return result;
         }
+        function request(m, u, d) {
+            var p = new Promise(), timeout;
+            self.logger.time(m + ' ' + u);
+            (function(xhr) {
+                xhr.onreadystatechange = function() {
+                    this.readyState ^ 4 || (self.logger.timeEnd(m + ' ' + u), clearTimeout(timeout),
p.done(null, this));
+                };
+                xhr.onerror=function(response){
+                    clearTimeout(timeout);
+                    p.done(response, null);
+                }
+                xhr.oncomplete=function(response){
+                    clearTimeout(timeout);
+                    self.logger.timeEnd(m + ' ' + u);
+                    self.info("%s request to %s returned %s", m, u, this.status );
+                }
+                xhr.open(m, u);
+                if (d) {
+                    if("object"===typeof d){
+                        d=JSON.stringify(d);
+                    }
+                    xhr.setRequestHeader("Content-Type", "application/json");
+                    xhr.setRequestHeader("Accept", "application/json");
+                }
+                timeout = setTimeout(function() {
+                    xhr.abort();
+                    p.done("API Call timed out.", null)
+                }, 30000);
+                //TODO stick that timeout in a config variable
+                xhr.send(encode(d));
+            }(new XMLHttpRequest()));
+            return p;
+        };
+        this.request=request;
+        this.get = partial(request,'GET');
+        this.post = partial(request,'POST');
+        this.put = partial(request,'PUT');
+        this.delete = partial(request,'DELETE');
+    }
     global[name] =  new Ajax();
     global[name].noConflict = function() {
         if(overwrittenName){

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/650cfd5c/sdks/html5-javascript/tests/mocha/test.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/tests/mocha/test.js b/sdks/html5-javascript/tests/mocha/test.js
index 50229e1..f3cb9e4 100644
--- a/sdks/html5-javascript/tests/mocha/test.js
+++ b/sdks/html5-javascript/tests/mocha/test.js
@@ -29,6 +29,76 @@ function usergridTestHarness(err, data, done, tests, ignoreError) {
 	}
 	done();
 }
+describe('Ajax', function() {
+    var dogName="dog"+Math.floor(Math.random()*10000);
+    var dogData=JSON.stringify({type:"dog",name:dogName});
+    var dogURI='https://api.usergrid.com/yourorgname/sandbox/dogs'
+    it('should POST to a URI',function(done){
+        Ajax.post(dogURI, dogData).then(function(err, data){
+            assert(!err, err);
+            done();
+        })
+    })
+    it('should GET a URI',function(done){
+        Ajax.get(dogURI+'/'+dogName).then(function(err, data){
+            assert(!err, err);
+            done();
+        })
+    })
+    it('should PUT to a URI',function(done){
+        Ajax.put(dogURI+'/'+dogName, {"favorite":true}).then(function(err, data){
+            assert(!err, err);
+            done();
+        })
+    })
+    it('should DELETE a URI',function(done){
+        Ajax.delete(dogURI+'/'+dogName, dogData).then(function(err, data){
+            assert(!err, err);
+            done();
+        })
+    })
+});
+describe('Usergrid Request/Response', function() {
+    var dogName="dog"+Math.floor(Math.random()*10000);
+    var dogData=JSON.stringify({type:"dog",name:dogName});
+    var dogURI='https://api.usergrid.com/yourorgname/sandbox/dogs'
+    it('should POST to a URI',function(done){
+        var req=new Usergrid.Request("POST", dogURI, {}, dogData, function(err, response){
+            assert(!err, err);
+            assert(response instanceof Usergrid.Response, "Response is not and instance of
Usergrid.Response");
+            done();
+        })
+    })
+    it('should GET a URI',function(done){
+        var req=new Usergrid.Request("GET", dogURI+'/'+dogName, {}, null, function(err, response){
+            assert(!err, err);
+            assert(response instanceof Usergrid.Response, "Response is not and instance of
Usergrid.Response");
+            done();
+        })
+    })
+    it('should PUT to a URI',function(done){
+        var req=new Usergrid.Request("PUT", dogURI+'/'+dogName, {}, {favorite:true}, function(err,
response){
+            assert(!err, err);
+            assert(response instanceof Usergrid.Response, "Response is not and instance of
Usergrid.Response");
+            done();
+        })
+    })
+    it('should DELETE a URI',function(done){
+        var req=new Usergrid.Request("DELETE", dogURI+'/'+dogName, {}, null, function(err,
response){
+            assert(!err, err);
+            assert(response instanceof Usergrid.Response, "Response is not and instance of
Usergrid.Response");
+            done();
+        })
+    })
+    it('should return a UsergridError object on an invalid URI',function(done){
+        var req=new Usergrid.Request("GET", dogURI+'/'+dogName+'zzzzz', {}, null, function(err,
response){
+            assert(err, "Should have returned an error");
+            assert(response instanceof Usergrid.Response, "Response is not and instance of
Usergrid.Response");
+            assert(err instanceof UsergridError, "Error is not and instance of UsergridError");
+            done();
+        })
+    })
+});
 describe('Usergrid', function() {
 	var client = getClient();
 	describe('Usergrid CRUD', function() {

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/650cfd5c/sdks/html5-javascript/usergrid.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/usergrid.js b/sdks/html5-javascript/usergrid.js
index 542fb38..7d4bc06 100644
--- a/sdks/html5-javascript/usergrid.js
+++ b/sdks/html5-javascript/usergrid.js
@@ -181,15 +181,10 @@ UsergridEventable.mixin = function(destObject) {
 //Ajax
 (function() {
     var name = "Ajax", global = this, overwrittenName = global[name], exports;
-    /*Function.prototype.partial = function() {
-        var fn = this,b = [].slice.call(arguments);
-        return fn.bind(undefined, b);
-    }*/
-    function partial(fn) {
-        var args = Array.prototype.slice.call(arguments, 1);
-        return function() {
-            return fn.apply(this, args.concat(Array.prototype.slice(arguments, 0)));
-        };
+    function partial() {
+        var args = Array.prototype.slice.call(arguments);
+        var fn = args.shift();
+        return fn.bind(this, args);
     }
     function Ajax() {
         this.logger = new global.Logger(name);
@@ -211,7 +206,6 @@ UsergridEventable.mixin = function(destObject) {
         function request(m, u, d) {
             var p = new Promise(), timeout;
             self.logger.time(m + " " + u);
-            self.logger.timeEnd(m + " " + u);
             (function(xhr) {
                 xhr.onreadystatechange = function() {
                     this.readyState ^ 4 || (self.logger.timeEnd(m + " " + u), clearTimeout(timeout),

@@ -223,10 +217,14 @@ UsergridEventable.mixin = function(destObject) {
                 };
                 xhr.oncomplete = function(response) {
                     clearTimeout(timeout);
+                    self.logger.timeEnd(m + " " + u);
                     self.info("%s request to %s returned %s", m, u, this.status);
                 };
                 xhr.open(m, u);
                 if (d) {
+                    if ("object" === typeof d) {
+                        d = JSON.stringify(d);
+                    }
                     xhr.setRequestHeader("Content-Type", "application/json");
                     xhr.setRequestHeader("Accept", "application/json");
                 }
@@ -494,14 +492,18 @@ function isFunction(f) {
  *  @return Returns whatever would be returned by the callback. or false.
  */
 function doCallback(callback, params, context) {
-    console.info("CALLED FROM", this.name || context ? context.name : "UNKNOWN");
     var returnValue;
     if (isFunction(callback)) {
         if (!params) params = [];
         if (!context) context = this;
         params.push(context);
-        //try {
-        returnValue = callback.apply(context, params);
+        try {
+            returnValue = callback.apply(context, params);
+        } catch (ex) {
+            if (console && console.error) {
+                console.error("Callback error:", ex);
+            }
+        }
     }
     return returnValue;
 }
@@ -688,10 +690,13 @@ function doCallback(callback, params, context) {
         var orgName = this.get("orgName");
         var appName = this.get("appName");
         var uri;
-        if (!mQuery && !orgName && !appName) {
+        var logoutCallback = function() {
             if (typeof this.logoutCallback === "function") {
                 return this.logoutCallback(true, "no_org_or_app_name_specified");
             }
+        }.bind(this);
+        if (!mQuery && !orgName && !appName) {
+            return logoutCallback();
         }
         if (mQuery) {
             uri = this.URI + "/" + endpoint;
@@ -702,7 +707,9 @@ function doCallback(callback, params, context) {
             qs.access_token = self.getToken();
         }
         var req = new Usergrid.Request(method, uri, qs, body, function(err, response) {
-            if ([ "auth_expired_session_token", "auth_missing_credentials", "auth_unverified_oath",
"expired_token", "unauthorized", "auth_invalid" ].indexOf(response.error) !== -1) {}
+            if ([ "auth_expired_session_token", "auth_missing_credentials", "auth_unverified_oath",
"expired_token", "unauthorized", "auth_invalid" ].indexOf(response.error) !== -1) {
+                return logoutCallback();
+            }
             doCallback(callback, [ err, response ]);
         });
     };
@@ -1329,6 +1336,8 @@ function doCallback(callback, params, context) {
     return global[name];
 })();
 
+var ENTITY_SYSTEM_PROPERTIES = [ "metadata", "created", "modified", "oldpassword", "newpassword",
"type", "activated", "uuid" ];
+
 /*
  *  A class to Model a Usergrid Entity.
  *  Set the type and uuid of entity in the 'data' json object
@@ -1470,7 +1479,7 @@ Usergrid.Entity.prototype.save = function(callback) {
     var self = this, type = this.get("type"), method = "POST", entityId = this.get("uuid"),
data = {}, entityData = this.get(), /*password = this.get('password'),
     oldpassword = this.get('oldpassword'),
     newpassword = this.get('newpassword'),*/
-    SYSTEM_PROPERTIES = [ "metadata", "created", "modified", "oldpassword", "newpassword",
"type", "activated", "uuid" ], options = {
+    options = {
         method: method,
         endpoint: type
     };
@@ -1482,7 +1491,7 @@ Usergrid.Entity.prototype.save = function(callback) {
     }
     //remove system-specific properties
     Object.keys(entityData).filter(function(key) {
-        return SYSTEM_PROPERTIES.indexOf(key) === -1;
+        return ENTITY_SYSTEM_PROPERTIES.indexOf(key) === -1;
     }).forEach(function(key) {
         data[key] = entityData[key];
     });
@@ -2394,7 +2403,7 @@ Usergrid.Group.prototype.fetch = function(callback) {
                 callback(err, data);
             }
         } else {
-            if (data.entities) {
+            if (data.entities && data.entities.length) {
                 var groupData = data.entities[0];
                 self._data = groupData || {};
                 self._client.request(memberOptions, function(err, data) {
@@ -2806,13 +2815,13 @@ Usergrid.Folder.prototype.fetch = function(callback) {
         if (!err) {
             self.getAssets(function(err, data) {
                 if (err) {
-                    doCallback(callback, [ true, new Usergrid.Error(data) ], self);
+                    doCallback(callback, [ true, new UsergridError(data) ], self);
                 } else {
                     doCallback(callback, [ null, self ], self);
                 }
             });
         } else {
-            doCallback(callback, [ true, new Usergrid.Error(data) ], self);
+            doCallback(callback, [ true, new UsergridError(data) ], self);
         }
     });
 };
@@ -2852,7 +2861,7 @@ Usergrid.Folder.prototype.addAsset = function(options, callback) {
         if (asset && asset instanceof Usergrid.Entity) {
             asset.fetch(function(err, data) {
                 if (err) {
-                    doCallback(callback, [ err, new Usergrid.Error(data) ], self);
+                    doCallback(callback, [ err, new UsergridError(data) ], self);
                 } else {
                     var endpoint = [ "folders", self.get("uuid"), "assets", asset.get("uuid")
].join("/");
                     var options = {
@@ -3004,7 +3013,7 @@ Usergrid.Asset.prototype.addToFolder = function(options, callback) {
             this._client.request(options, callback);
         });
     } else {
-        doCallback(callback, [ true, new Usergrid.Error("folder not specified") ], self);
+        doCallback(callback, [ true, new UsergridError("folder not specified") ], self);
     }
 };
 
@@ -3018,7 +3027,7 @@ Usergrid.Asset.prototype.addToFolder = function(options, callback) {
  */
 Usergrid.Asset.prototype.upload = function(data, callback) {
     if (!(window.File && window.FileReader && window.FileList &&
window.Blob)) {
-        return doCallback(callback, [ true, new Usergrid.Error("The File APIs are not fully
supported by your browser.") ], self);
+        return doCallback(callback, [ true, new UsergridError("The File APIs are not fully
supported by your browser.") ], self);
     }
     var self = this;
     var endpoint = [ this._client.URI, this._client.orgName, this._client.appName, "assets",
self.get("uuid"), "data" ].join("/");
@@ -3027,11 +3036,11 @@ Usergrid.Asset.prototype.upload = function(data, callback) {
     xhr.open("POST", endpoint, true);
     xhr.onerror = function(err) {
         //callback(true, err);
-        doCallback(callback, [ true, new Usergrid.Error("The File APIs are not fully supported
by your browser.") ], self);
+        doCallback(callback, [ true, new UsergridError("The File APIs are not fully supported
by your browser.") ], self);
     };
     xhr.onload = function(ev) {
         if (xhr.status >= 300) {
-            doCallback(callback, [ true, new Usergrid.Error(JSON.parse(xhr.responseText))
], self);
+            doCallback(callback, [ true, new UsergridError(JSON.parse(xhr.responseText))
], self);
         } else {
             doCallback(callback, [ null, self ], self);
         }
@@ -3067,7 +3076,7 @@ Usergrid.Asset.prototype.download = function(callback) {
     };
     xhr.onerror = function(err) {
         callback(true, err);
-        doCallback(callback, [ true, new Usergrid.Error(err) ], self);
+        doCallback(callback, [ true, new UsergridError(err) ], self);
     };
     xhr.send();
 };


Mime
View raw message