From f3f68af0abe3235ed88198b57c4236054fbcd1d0 Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Thu, 1 Dec 2011 00:46:45 -0700
Subject: [PATCH] (loadgen) fix several cases where loadgen would crash on
 server error responses

---
 lib/load_gen/activities/add_email.js  |  4 ++--
 lib/load_gen/activities/reset_pass.js | 12 ++++++++----
 lib/load_gen/activities/signup.js     | 14 +++++++++-----
 3 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/lib/load_gen/activities/add_email.js b/lib/load_gen/activities/add_email.js
index 6ec6a4bd8..ba02e14ac 100644
--- a/lib/load_gen/activities/add_email.js
+++ b/lib/load_gen/activities/add_email.js
@@ -87,7 +87,7 @@ exports.startFunc = function(cfg, cb) {
       email: email,
       site: userdb.any(user.sites)
     }, function (r) {
-      if (r.code !== 200) {
+      if (!r || r.code !== 200) {
         var msg = 'failed to add email: ' + email + ' to existing user ' +
           user.emails[0];
         winston.error(msg);
@@ -97,7 +97,7 @@ exports.startFunc = function(cfg, cb) {
       wcli.get(cfg, '/wsapi/fake_verification', context, {
         email: email
       }, function (r) {
-        if (r.code !== 200) {
+        if (!r || r.code !== 200) {
           var err ='failed to fetch verification token for email: ' + email;
           winston.error(err);
           return cb(err);
diff --git a/lib/load_gen/activities/reset_pass.js b/lib/load_gen/activities/reset_pass.js
index 4f4d02a58..e051eca16 100644
--- a/lib/load_gen/activities/reset_pass.js
+++ b/lib/load_gen/activities/reset_pass.js
@@ -80,21 +80,25 @@ exports.startFunc = function(cfg, cb) {
     email: email,
     site: userdb.any(user.sites)
   }, function (r) {
-    if (r.code !== 200) return cb(false);
+    if (!r || r.code !== 200) return cb(false);
     // now get the verification secret
     wcli.get(cfg, '/wsapi/fake_verification', context, {
       email: email
     }, function (r) {
-      if (r.code !== 200) return cb(false);
+      if (!r || r.code !== 200) return cb(false);
       // and simulate clickthrough
       wcli.post(cfg, '/wsapi/complete_user_creation', context, {
         token: r.body,
         pass: user.password
       }, function (r) {
-        r.body = JSON.parse(r.body);
-        if (r.code !== 200 || r.body.success !== true) {
+        if (!r || r.code !== 200) {
           return cb("failed to complete user creation");
         }
+        try {
+          if (JSON.parse(r.body).success !== true) throw "failed";
+        } catch(e) {
+          return cb("failed to complete user creation (body doesn't have .success === true)");
+        }
         // and now let's log in with this email address
         common.authAndKey(cfg, user, context, email, function(err) {
           if (err) return cb(err);
diff --git a/lib/load_gen/activities/signup.js b/lib/load_gen/activities/signup.js
index cd62201a2..0ea44f1c2 100644
--- a/lib/load_gen/activities/signup.js
+++ b/lib/load_gen/activities/signup.js
@@ -95,20 +95,24 @@ exports.startFunc = function(cfg, cb) {
     email: email,
     site: userdb.any(user.sites)
   }, function (r) {
-    if (r.code !== 200) return cb(false);
+    if (!r || r.code !== 200) return cb(false);
     // now get the verification secret
     wcli.get(cfg, '/wsapi/fake_verification', context, {
       email: email
     }, function (r) {
-      if (r.code !== 200) return cb(false);
+      if (!r || r.code !== 200) return cb(false);
       // and simulate clickthrough
       wcli.post(cfg, '/wsapi/complete_user_creation', context, {
         token: r.body,
         pass: user.password
       }, function (r) {
-        r.body = JSON.parse(r.body);
-        if (r.code !== 200 || r.body.success !== true) {
-          return cb("failed to complete user creation");
+        try {
+          r.body = JSON.parse(r.body);
+          if (r.code !== 200 || r.body.success !== true) {
+            throw "non-success";
+          }
+        } catch(e) {
+          return cb("failed to complete user creation: " + e);
         }
         // and now let's log in with this email address
         common.authAndKey(cfg, user, context, email, function(err) {
-- 
GitLab