Skip to content

Commit c763b00

Browse files
mheveryIgorMinar
authored andcommitted
feat(gdocs.js): output usefull error messages when not logged in.
1 parent e5da0c9 commit c763b00

1 file changed

Lines changed: 31 additions & 10 deletions

File tree

gdocs.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function fetch(collection, url){
5454
if (title.match(/\.ngdoc$/)) {
5555
var exportUrl = entry.match(/<content type='text\/html' src='(.*?)'\/>/)[1];
5656
download(collection, title, exportUrl);
57-
}
57+
};
5858
});
5959
}
6060
);
@@ -136,7 +136,13 @@ function login(username, password){
136136
}
137137

138138
function getAuthToken(){
139-
return fs.readFileSync('tmp/gdocs.auth');
139+
var pwdFile = 'tmp/gdocs.auth';
140+
try {
141+
fs.statSync(pwdFile);
142+
return fs.readFileSync(pwdFile);
143+
} catch (e) {
144+
console.log('Please log in first...');
145+
}
140146
}
141147

142148
function request(method, url, options, response) {
@@ -147,14 +153,29 @@ function request(method, url, options, response) {
147153
path: url[3],
148154
method: method
149155
}, function(res){
150-
var data = [];
151-
res.setEncoding('utf8');
152-
res.on('end', function(){
153-
response(data.join(''));
154-
});
155-
res.on('data', function (chunk) {
156-
data.push(chunk);
157-
});
156+
switch (res.statusCode) {
157+
case 200: {
158+
var data = [];
159+
res.setEncoding('utf8');
160+
res.on('end', function(){
161+
response(data.join(''));
162+
});
163+
res.on('data', function (chunk) {
164+
data.push(chunk);
165+
});
166+
res.on('error', function(e){
167+
console.log(e);
168+
});
169+
break;
170+
}
171+
case 401: {
172+
console.log('Eror: Login credentials expired! Please login.');
173+
break;
174+
}
175+
default: {
176+
console.log(res);
177+
}
178+
}
158179
});
159180
for(var header in options.headers) {
160181
request.setHeader(header, options.headers[header]);

0 commit comments

Comments
 (0)