Skip to content

Commit 5cc2a0f

Browse files
Merge pull request #34 from MaartenDesnouck/better-error-logging-handling-and-feedback
Better error logging, handling and feedback
2 parents 47783b1 + ff372cc commit 5cc2a0f

25 files changed

+261
-154
lines changed

lib/auth.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var authenticate = require('./functions/authenticate.js');
2+
var handleError = require('./functions/handleError.js');
3+
var colors = require('colors');
24

35
/**
46
* Get the metadata of a file with a given id
@@ -8,9 +10,9 @@ var authenticate = require('./functions/authenticate.js');
810
module.exports = function(options) {
911
authenticate(options, function(err, token) {
1012
if (err) {
11-
console.log('gas returned an error: ' + err);
13+
handleError(err);
1214
} else {
13-
console.log('You are succesfully authenticated.');
15+
console.log('You are succesfully authenticated [' +'✔'.green + ']');
1416
}
1517
});
1618
}

lib/clone.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,43 @@ var authenticate = require('./functions/authenticate.js');
22
var downloadRemote = require('./functions/downloadRemote.js');
33
var unpackRemote = require('./functions/unpackRemote.js');
44
var getMetadata = require('./functions/getMetadata.js');
5+
var handleError = require('./functions/handleError.js');
56
var constants = require('./constants.js');
67
var fs = require('fs');
8+
var colors = require('colors');
9+
var readline = require('readline');
710

811
/**
912
* Clone a remote Google Apps Script project
1013
*
1114
* @param {string} fileId - Id of the project we want to clone.
1215
*/
1316
module.exports = function(identifier) {
14-
console.log('Cloning project from Google Drive...');
17+
process.stdout.write('Cloning from Google Drive...');
1518
authenticate([], function(err, oauth2Client) {
1619
if (err) {
17-
console.log('gas returned an error: ' + err);
20+
process.stdout.write(' [' + '✘'.red + ']\n');
21+
handleError(err);
1822
} else {
1923
getMetadata(oauth2Client, identifier, function(err, metadata) {
2024
if (err) {
21-
console.log('gas returned an error: ' + err);
25+
process.stdout.write(' [' + '✘'.red + ']\n');
26+
handleError(err);
2227
} else {
28+
process.stdout.clearLine();
29+
readline.cursorTo(process.stdout, 0);
30+
process.stdout.write('Cloning \'' + metadata.name + '\' from Google Drive...');
2331
downloadRemote(oauth2Client, metadata.id, metadata.name, 'clone', function(err) {
2432
if (err) {
25-
console.log('gas returned an error: ' + err);
33+
process.stdout.write(' [' + '✘'.red + ']\n');
34+
handleError(err);
2635
} else {
2736
unpackRemote(metadata.name, function(err) {
2837
if (err) {
29-
console.log('gas returned an error: ' + err);
38+
process.stdout.write(' [' + '✘'.red + ']\n');
39+
handleError(err);
3040
} else {
31-
console.log('Succesfully cloned from Google Drive.');
41+
process.stdout.write(' [' + '✔'.green + ']\n');
3242
}
3343
});
3444
}

lib/functions/authenticate.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var google = require('googleapis');
44
var googleAuth = require('google-auth-library');
55
var open = require('open');
66
var constants = require('../constants.js');
7+
var handleError = require('./handleError.js');
78

89
/**
910
* Create an OAuth2 client with the given credentials, and then execute the
@@ -81,7 +82,7 @@ function getNewToken(oauth2Client, callback) {
8182
rl.close();
8283
oauth2Client.getToken(code, function(err, token) {
8384
if (err) {
84-
console.log('Error while trying to retrieve access token.', err);
85+
handleError(err);
8586
return;
8687
}
8788
oauth2Client.credentials = token;

lib/functions/downloadIncludedFiles.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
var google = require('googleapis');
22
var fs = require('fs');
3+
var request = require('request');
34
var rimrafAll = require('./rimrafAll.js');
45
var constants = require('../constants.js');
5-
var request = require('request');
66

77
var includeDir = '';
88

lib/functions/downloadRemote.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ function downloadRemote(auth, fileId, dir, method, callback) {
2323

2424
switch (method) {
2525
case 'clone':
26-
if (fs.existsSync(gasDir)) {
27-
callback('Oops, you seem to be cloning in a directory already containing a .gas folder. \n' +
28-
'Please check your project strucure and try again.');
29-
return;
30-
} else if (fs.existsSync('./' + dir)) {
26+
if (fs.existsSync('./' + dir)) {
3127
callback('Oops, the directory \'' + dir + '\' seems to exist already. \n' +
3228
'Remove this folder or use \'gas link\' to link your project to the correct folder.');
3329
return;

lib/functions/getId.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ function getId(callback) {
1010
var uri = constants.META_DIR + '/' + constants.META_ID;
1111
fs.readFile(uri, 'utf8', function(err, data) {
1212
if (err) {
13-
if (err.code == 'ENOENT') {
14-
err = 'There appears to be no project linked to this folder. \n' +
15-
'Navigate to a project folder or execute \'gas new <name>\',' +
16-
' \'gas clone <fileId>\' or \'gas link <fileId>\' to get started.'
17-
}
1813
callback(err, null, null);
1914
return;
2015
} else {

lib/functions/getMetadata.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var google = require('googleapis');
22
var fs = require('fs');
3-
var constants = require('../constants.js');
43
var sanitize = require('sanitize-filename');
4+
var colors = require('colors');
5+
var constants = require('../constants.js');
56
var listScriptFiles = require('./listScriptFiles.js');
67

78
/**
@@ -28,8 +29,9 @@ function getMetadata(auth, identifier, callback) {
2829
return;
2930
} else {
3031
if (files.length === 0) { // 0 results
31-
console.log('No project with name or id \'' + identifier + '\' found in your Google Drive.');
32-
console.log('Use \'gas list\' to show all the projects in your Google Drive.')
32+
callback('HANDELED');
33+
console.log('No project with name or id \'' + identifier + '\' found in Google Drive [' + '✘'.red + ']');
34+
console.log('Use \'gas list\' to show all the projects in Google Drive.');
3335
} else if (files.length === 1) { // 1 result
3436
var result = files[0];
3537
if (result.name === identifier) {
@@ -38,7 +40,9 @@ function getMetadata(auth, identifier, callback) {
3840
return;
3941
} else {
4042
// Check for exact match if exists
41-
console.log('No exact match found in your Google Drive. Did you perhaps mean: \'' + result.name + '\'?')
43+
callback('HANDELED');
44+
console.log('No exact match found in Google Drive [' + '✘'.red + ']');
45+
console.log('Did you perhaps mean: \'' + result.name + '\'?');
4246
}
4347
} else { // More than 1 result
4448
var exactMatches = [];
@@ -48,7 +52,8 @@ function getMetadata(auth, identifier, callback) {
4852
}
4953
}
5054
if (exactMatches.length === 0) { // 0 results
51-
console.log('No project called \'' + identifier + '\' found in your Google Drive.');
55+
callback('HANDELED');
56+
console.log('No project called \'' + identifier + '\' found in Google Drive [' + '✘'.red + ']');
5257
console.log('Did you mean one of these projects? :');
5358
for (result in files) {
5459
console.log("[%s] %s", files[result].id, files[result].name);
@@ -58,7 +63,8 @@ function getMetadata(auth, identifier, callback) {
5863
callback(null, exactMatches[0]);
5964
return;
6065
} else {
61-
console.log('Multiple projects called \'' + identifier + '\' found in your Google Drive.');
66+
callback('HANDELED');
67+
console.log('Multiple projects called \'' + identifier + '\' found in Google Drive [' + '✘'.red + ']');
6268
console.log('Use \'gas rename <fileId> <newName>\' to rename projects so they have a unique name or use the fileId as identifier');
6369
for (result in files) {
6470
console.log("[%s] %s", files[result].id, files[result].name);

lib/functions/handleError.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var fs = require('fs');
2+
var colors = require('colors');
3+
var constants = require('../constants.js');
4+
5+
/**
6+
* Handle error in an appropriate hanner
7+
*
8+
* @param {err} string - The error to handle.
9+
*/
10+
function handleError(err) {
11+
if (err == 'HANDELED') {
12+
// Do nothing
13+
} else if (err == 'Error: Invalid Credentials') {
14+
console.log('✘'.red + ' Your credentials appear to be invalid.');
15+
console.log('Run \'gas auth -f\' to re-authenicate.');
16+
} else if (err == 'Error: invalid_grant') {
17+
console.log('✘'.red + ' Your credentials appear to be invalid.');
18+
console.log('Run \'gas auth -f\' to re-authenicate.');
19+
} else if (err.code == 'ENOENT' && err.path == '.gas/ID') {
20+
console.log('✘'.red + ' There appears to be no project linked to this folder. \n' +
21+
'Navigate to a project folder or execute \'gas new <name>\',' +
22+
' \'gas clone <fileId>\' or \'gas link <fileId>\' to get started.');
23+
} else if (err.code == 'ENOENT' && err.path == './gas-include.js') {
24+
console.log('There appears to be no \'gas-include.js\' file in this folder.');
25+
} else {
26+
console.log('gas returned an error: ' + err);
27+
}
28+
}
29+
30+
module.exports = handleError;

lib/functions/listScriptFiles.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var google = require('googleapis');
2+
var colors = require('colors');
23
var constants = require('../constants.js');
34

45
/**
@@ -30,10 +31,6 @@ function listScriptFiles(auth, nameFilter, display, nextPageToken, callback) {
3031
pageToken: nextPageToken,
3132
}, function(err, response) {
3233
if (err) {
33-
console.log('The API returned an error: ' + err);
34-
if (err == 'Error: Invalid Credentials') {
35-
console.log('Run \'gas auth -f\' to re-authenicate.');
36-
}
3734
callback(err);
3835
return;
3936
}
@@ -43,9 +40,9 @@ function listScriptFiles(auth, nameFilter, display, nextPageToken, callback) {
4340
if (display) {
4441
if (nextPageToken == null && files.length === 0) {
4542
if (nameFilter == null) {
46-
console.log("No script projects found in your Google Drive.");
43+
console.log('No script projects found in Google Drive [' + '✘'.red + ']');
4744
} else {
48-
console.log("No script projects matching your filter found in your Google Drive.");
45+
console.log('No script projects matching your filter found in Google Drive [' + '✘'.red + ']');
4946
}
5047
}
5148

lib/functions/parseIncludeFile.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@ function parseIncludeFile(callback) {
1414
fs.readFile(includeFile, 'utf8', function(err, content) {
1515
if (err) {
1616
if (err.code == 'ENOENT') {
17-
err = 'There appears to be no \'' + constants.INCLUDE_FILE + '\' file in this folder.'
17+
// Include file does not exist yet
18+
content = "// Example content of gas-include.js\n" +
19+
"// \"time.js\" - \"https://raw.githubusercontent.com/MaartenDesnouck/google-apps-script-include/master/Time.js\"\n";
20+
21+
fs.writeFile(includeFile, content, function() {});
22+
callback(null, []);
23+
return;
24+
} else {
25+
callback(err);
26+
return;
1827
}
19-
callback(err);
20-
return;
2128
} else {
2229

2330
// Parse content of include file

0 commit comments

Comments
 (0)