Commits

Andreas Knecht committed 092cedd

Better error handling.

Comments (0)

Files changed (1)

lib/passport-atlassian-crowd/strategy.js

     }
 
     function handleGroupResponse(response, result) {
-        if(response.statusCode !== 200) {
-            return self.fail();
-        } else {
+        if (response.statusCode === 200) {
             var resultObject = JSON.parse(result);
-            var groups =[];
-            resultObject.groups.forEach(function(group) {
+            var groups = [];
+            resultObject.groups.forEach(function (group) {
                 groups.push(group.GroupEntity.name);
             });
 
             return groups;
+
+        } else if (response.statusCode >= 400 && response.statusCode < 500) {
+            var error = JSON.parse(result);
+            console.log("Error retrieving groups for user '" + username + "': " + error.message + " [" + error.reason + "]");
+            return self.fail();
+        } else {
+            return self.error(new Error("Invalid response from Crowd Server '" + self._crowdServer +
+                "' [" + response.statusCode + "]: " + result));
         }
     }
 
     function handleAuthenticationResponse(response, result) {
-        var resultObject = JSON.parse(result);
-        console.log("Got Status [" + response.statusCode + "]: " + JSON.stringify(resultObject));
-        if (response.statusCode !== 200) {
-            return self.fail();
-        } else {
+        if (response.statusCode === 200) {
+            var crowdUser = JSON.parse(result);
             var userprofile = {
                 provider:'atlassian-crowd',
-                id:resultObject.name,
-                username:resultObject.name,
-                displayName:resultObject["display-name"],
+                id:crowdUser.name,
+                username:crowdUser.name,
+                displayName:crowdUser["display-name"],
                 name:{
-                    familyName:resultObject["last-name"],
-                    givenName:resultObject["first-name"]
+                    familyName:crowdUser["last-name"],
+                    givenName:crowdUser["first-name"]
                 },
-                email:resultObject.email,
+                email:crowdUser.email,
                 emails:[
-                    {value:resultObject.email}
+                    {value:crowdUser.email}
                 ],
                 _raw:result,
-                _json:resultObject
+                _json:crowdUser
             };
 
             if (self._retrieveGroupMemberships) {
             } else {
                 return self._verify(userprofile, verified);
             }
+        } else if (response.statusCode >= 400 && response.statusCode < 500) {
+            var error = {"message":result};
+            try {
+                error = JSON.parse(result);
+            } catch (err) {
+            }
+
+            var logMsg = "Error authenticating user '" + username + "' on '" + self._crowdServer + "': " + error.message;
+            if (error.reason) {
+                logMsg += " [" + error.reason + "]";
+            }
+            console.log(logMsg);
+            return self.fail();
+        } else {
+            return self.error(new Error("Invalid response from Crowd Server '" + self._crowdServer +
+                "' [" + response.statusCode + "]: " + result));
         }
     }