diff options
author | przemyslawpluta <przemekpluta@hotmail.com> | 2015-08-07 11:58:05 +0100 |
---|---|---|
committer | przemyslawpluta <przemekpluta@hotmail.com> | 2015-08-07 11:58:05 +0100 |
commit | d765f685542c937ad2bde64ac237034083d06106 (patch) | |
tree | 6e0b444794a782f0d50ab64a05f85e4b76cc79ae /lib | |
parent | 01313a61ef31628cbb4abd0d5a3c9cc78a8b893a (diff) | |
download | mongo-edu-d765f685542c937ad2bde64ac237034083d06106.zip |
better error handling
Diffstat (limited to 'lib')
-rw-r--r-- | lib/login.js | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/lib/login.js b/lib/login.js index 6da23b7..29ab9a3 100644 --- a/lib/login.js +++ b/lib/login.js @@ -11,9 +11,11 @@ var request = require('request'), ProgressBar = require('progress'), _ = require('lodash'), videoHandler = require('./videos'), + PrettyError = require('pretty-error'), coursewareHandler = require('./courseware'); -var jar = request.jar(), host = 'https://university.mongodb.com', xcsrftoken = {}; +var jar = request.jar(), host = 'https://university.mongodb.com', + pe = new PrettyError(), xcsrftoken = {}; function addCookies(cookies, url) { @@ -48,7 +50,7 @@ module.exports = { if (res.statusCode === 200) { var status = JSON.parse(body); - if (status.success !== true) { return callback(new Error(status.value)); } + if (status.success !== true) { return console.log('i'.red + ' ' + status.value + '\n'); } bar.tick(); @@ -56,7 +58,7 @@ module.exports = { } - callback(new Error(res.statusCode)); + callback(pe.render(new Error(res.statusCode))); }); }, @@ -64,7 +66,9 @@ module.exports = { checkProfile = function checkProfile(bar) { request({ url: url + '/edit_profile', jar: jar }, function get(err, res, body) { if (err !== null) { return callback(err, null); } + bar.tick(); + if (res.statusCode === 200) { bar.tick(); @@ -84,6 +88,8 @@ module.exports = { return checkCourses(bar, currentProfile); } + + callback(pe.render(new Error(res.statusCode))); }); }, @@ -115,9 +121,11 @@ module.exports = { if (noCourses) { currentProfile.noCourses = noCourses.trim().split('\n').map(function items(item) { return item.replace(/ +/g, ' '); }).join('') + ' at ' + url + findNewCourses + '.\n'; } - callback(null, list, currentProfile); + return callback(null, list, currentProfile); } + callback(pe.render(new Error(res.statusCode))); + }); }; @@ -135,7 +143,7 @@ module.exports = { } - callback(new Error(res.statusCode)); + callback(pe.render(new Error(res.statusCode))); }); }, @@ -187,6 +195,8 @@ module.exports = { } + callback(pe.render(new Error(res.statusCode))); + }); }, @@ -224,7 +234,7 @@ module.exports = { return callback(null, videos); } - callback(new Error(res.statusCode)); + callback(pe.render(new Error(res.statusCode))); }); }, @@ -259,7 +269,8 @@ module.exports = { }, function post(err, res, body) { if (err !== null) { return callback(err, null); } - if (res.statusCode === 200) { callback(null, JSON.parse(body)); } + if (res.statusCode === 200) { return callback(null, JSON.parse(body)); } + callback(pe.render(new Error(res.statusCode))); }); } |