Skip to content

Commit

Permalink
feat(gdocs.js): output usefull error messages when not logged in.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery authored and IgorMinar committed Aug 31, 2011
1 parent e5da0c9 commit c763b00
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions gdocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function fetch(collection, url){
if (title.match(/\.ngdoc$/)) {
var exportUrl = entry.match(/<content type='text\/html' src='(.*?)'\/>/)[1];
download(collection, title, exportUrl);
}
};
});
}
);
Expand Down Expand Up @@ -136,7 +136,13 @@ function login(username, password){
}

function getAuthToken(){
return fs.readFileSync('tmp/gdocs.auth');
var pwdFile = 'tmp/gdocs.auth';
try {
fs.statSync(pwdFile);
return fs.readFileSync(pwdFile);
} catch (e) {
console.log('Please log in first...');
}
}

function request(method, url, options, response) {
Expand All @@ -147,14 +153,29 @@ function request(method, url, options, response) {
path: url[3],
method: method
}, function(res){
var data = [];
res.setEncoding('utf8');
res.on('end', function(){
response(data.join(''));
});
res.on('data', function (chunk) {
data.push(chunk);
});
switch (res.statusCode) {
case 200: {
var data = [];
res.setEncoding('utf8');
res.on('end', function(){
response(data.join(''));
});
res.on('data', function (chunk) {
data.push(chunk);
});
res.on('error', function(e){
console.log(e);
});
break;
}
case 401: {
console.log('Eror: Login credentials expired! Please login.');
break;
}
default: {
console.log(res);
}
}
});
for(var header in options.headers) {
request.setHeader(header, options.headers[header]);
Expand Down

0 comments on commit c763b00

Please sign in to comment.