Rework callback error handling

This commit is contained in:
Tim Schaub 2015-12-08 09:20:08 -07:00
parent 0794c0b142
commit ab7291a60c
3 changed files with 27 additions and 24 deletions

View file

@ -25,12 +25,12 @@ ghpages.publish(path.join(process.cwd(), program.dist), {
dotfiles: !!program.dotfiles,
add: !!program.add,
logger: function(message) {
console.log(message);
process.stderr.write(message + '\n');
}
}, function(err) {
if (err != null) {
console.log(err)
if (err) {
process.stderr.write(err.message + '\n');
return process.exit(1);
}
console.log('Published');
process.stderr.write('Published\n');
});

View file

@ -50,25 +50,12 @@ function getRepo(options) {
* @param {Object} config Publish options.
* @param {Function} callback Callback.
*/
/*eslint-disable no-console */
exports.publish = function publish(basePath, config, callback) {
if (typeof config === 'function') {
callback = config;
config = {};
}
var done = function(arguments) {
try {
(callback || function(err) {
if (err) {
console.log(err);
}
})(arguments);
} catch (err) {
console.error('errors during calling the callback: ', err);
}
}
var defaults = {
add: false,
git: 'git',
@ -94,6 +81,28 @@ exports.publish = function publish(basePath, config, callback) {
options[c] = config[c];
}
function log(message) {
if (!options.silent) {
options.logger(message);
}
}
if (!callback) {
callback = function(err) {
if (err) {
log(err.message);
}
};
}
function done(err) {
try {
callback(err);
} catch (err2) {
log('Publish callback threw: ', err2.message);
}
}
try {
if (!fs.statSync(basePath).isDirectory()) {
done(new Error('The "base" option must be an existing directory'));
@ -118,12 +127,6 @@ exports.publish = function publish(basePath, config, callback) {
var only = glob.sync(options.only, {cwd: basePath});
function log(message) {
if (!options.silent) {
options.logger(message);
}
}
git.exe(options.git);
var repoUrl;

View file

@ -22,7 +22,7 @@
},
"main": "lib/index.js",
"scripts": {
"pretest": "eslint lib test",
"pretest": "eslint lib test bin/gh-pages",
"test": "mocha --recursive test"
},
"dependencies": {