Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 29d36e9

Browse files
mheveryIgorMinar
authored andcommitted
feat(gdocs): better error handling
1 parent 091c173 commit 29d36e9

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

gdocs.js

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,36 @@ var https = require('https');
55
var fs = require('fs');
66

77
var collections = {
8-
'guide': 'http://docs.google.com/feeds/default/private/full/folder%3A0B9PsajIPqzmANGUwMGVhZmYtMTk1ZC00NTdmLWIxMDAtZGI5YWNlZjQ2YjZl/contents',
9-
'api': 'http://docs.google.com/feeds/default/private/full/folder%3A0B7Ovm8bUYiUDYjMwYTc2YWUtZTgzYy00YjIxLThlZDYtYWJlOTFlNzE2NzEw/contents',
10-
'tutorial': 'http://docs.google.com/feeds/default/private/full/folder%3A0B9PsajIPqzmAYWMxYWE3MzYtYzdjYS00OGQxLWJhZjItYzZkMzJiZTRhZjFl/contents',
11-
'cookbook': 'http://docs.google.com/feeds/default/private/full/folder%3A0B7Ovm8bUYiUDNzkxZWM5ZTItN2M5NC00NWIxLTg2ZDMtMmYwNDY1NWM1MGU4/contents',
12-
'misc': 'http://docs.google.com/feeds/default/private/full/folder%3A0B7Ovm8bUYiUDZjVlNmZkYzQtMjZlOC00NmZhLWI5MjAtMGRjZjlkOGJkMDBi/contents'
8+
'guide': 'https://docs.google.com/feeds/default/private/full/folder%3A0B9PsajIPqzmANGUwMGVhZmYtMTk1ZC00NTdmLWIxMDAtZGI5YWNlZjQ2YjZl/contents',
9+
'api': 'https://docs.google.com/feeds/default/private/full/folder%3A0B7Ovm8bUYiUDYjMwYTc2YWUtZTgzYy00YjIxLThlZDYtYWJlOTFlNzE2NzEw/contents',
10+
'tutorial': 'https://docs.google.com/feeds/default/private/full/folder%3A0B9PsajIPqzmAYWMxYWE3MzYtYzdjYS00OGQxLWJhZjItYzZkMzJiZTRhZjFl/contents',
11+
'cookbook': 'https://docs.google.com/feeds/default/private/full/folder%3A0B7Ovm8bUYiUDNzkxZWM5ZTItN2M5NC00NWIxLTg2ZDMtMmYwNDY1NWM1MGU4/contents',
12+
'misc': 'https://docs.google.com/feeds/default/private/full/folder%3A0B7Ovm8bUYiUDZjVlNmZkYzQtMjZlOC00NmZhLWI5MjAtMGRjZjlkOGJkMDBi/contents'
1313
};
1414

1515
console.log('Google Docs...');
1616

1717
var flag = process && process.argv[2];
18-
if (flag == '--login')
19-
askPassword(function(password){
20-
login(process.argv[3], password);
21-
});
22-
else if (flag == '--fetch') {
18+
if (flag == '--login') {
19+
var username = process.argv[3];
20+
if (username) {
21+
askPassword(function(password){
22+
login(username, password);
23+
});
24+
} else {
25+
console.log('Missing username!');
26+
}
27+
} else if (flag == '--fetch') {
2328
var collection = process.argv[3];
2429
if (collection) {
2530
fetch(collection, collections[collection]);
2631
} else {
2732
for (collection in collections)
2833
fetch(collection, collections[collection]);
2934
}
30-
} else
35+
} else {
3136
help();
37+
}
3238

3339
function help(){
3440
console.log('Synopsys');
@@ -147,34 +153,36 @@ function getAuthToken(){
147153

148154
function request(method, url, options, response) {
149155
var url = url.match(/http(s?):\/\/(.+?)(\/.*)/);
150-
var request = (url[1] ? https : http).request({
156+
var isHttps = url[1];
157+
var request = (isHttps ? https : http).request({
151158
host: url[2],
152159
port: (url[1] ? 443 : 80),
153160
path: url[3],
154161
method: method
155162
}, function(res){
156163
switch (res.statusCode) {
157-
case 200: {
164+
case 200:
158165
var data = [];
159166
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-
});
167+
res.on('end', function (){ response(data.join('')); });
168+
res.on('close', function (){ response(data.join('')); }); // https
169+
res.on('data', function (chunk) { data.push(chunk); });
170+
res.on('error', function (e){ console.log(e); });
169171
break;
170-
}
171-
case 401: {
172+
case 401:
172173
console.log('Eror: Login credentials expired! Please login.');
173174
break;
174-
}
175-
default: {
176-
console.log(res);
177-
}
175+
default:
176+
var data = [];
177+
console.log('ERROR: ', res.statusCode);
178+
console.log('REQUEST URL: ', url[0]);
179+
console.log('REQUEST POST: ', options.data);
180+
console.log('REQUEST HEADERS: ', options.headers);
181+
console.log('RESPONSE HEADERS: ', res.headers);
182+
res.on('end', function (){ console.log('BODY: ', data.join('')); });
183+
res.on('close', function (){ console.log('BODY: ', data.join('')); }); // https
184+
res.on('data', function (chunk) { data.push(chunk); });
185+
res.on('error', function (e){ console.log(e); });
178186
}
179187
});
180188
for(var header in options.headers) {

0 commit comments

Comments
 (0)