diff --git a/lib/CLI.js b/lib/CLI.js index 5adae24c..76988fdc 100644 --- a/lib/CLI.js +++ b/lib/CLI.js @@ -152,7 +152,7 @@ CLI._startScript = function(script, opts, cb) { conf.script = script; if ((appConf = verifyConfs(conf)) instanceof Error) - return cb ? cb(appConf) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(appConf)) : Common.exitCli(cst.ERROR_EXIT); conf = appConf[0]; @@ -244,7 +244,7 @@ CLI._startScript = function(script, opts, cb) { resolved_paths = resolvePaths(conf); } catch(e) { Common.printError(e); - return cb(e); + return cb(retErr(e)); } Common.printOut(cst.PREFIX_MSG + 'Starting %s in %s (%d instance' + (resolved_paths.instances > 1 ? 's' : '') + ')', @@ -257,7 +257,7 @@ CLI._startScript = function(script, opts, cb) { Satan.executeRemote('prepare', resolved_paths, function(err, data) { if (err) { Common.printError(cst.PREFIX_MSG_ERR + 'Error while launching application', err.stack || err); - return cb(new Error(err)); + return cb(retErr(err)); } Common.printOut(cst.PREFIX_MSG + 'Done.'); @@ -311,7 +311,7 @@ CLI._startJson = function(file, opts, action, pipe, cb) { data = fs.readFileSync(file); } catch(e) { Common.printError(cst.PREFIX_MSG_ERR + 'File ' + file +' not found'); - return cb ? cb(e) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(e)) : Common.exitCli(cst.ERROR_EXIT); } try { @@ -319,7 +319,7 @@ CLI._startJson = function(file, opts, action, pipe, cb) { } catch(e) { Common.printError(cst.PREFIX_MSG_ERR + 'File ' + file + ' malformated'); console.error(e); - return cb ? cb(e) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(e)) : Common.exitCli(cst.ERROR_EXIT); } } @@ -351,7 +351,7 @@ CLI._startJson = function(file, opts, action, pipe, cb) { Satan.executeRemote('getMonitorData', {}, function(err, raw_proc_list) { if (err) { Common.printError(err); - return cb ? cb(new Error(err)) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); } /** @@ -406,7 +406,7 @@ CLI._startJson = function(file, opts, action, pipe, cb) { }); }, function(err) { - if (err) return cb ? cb(new Error(err)) : Common.exitCli(cst.ERROR_EXIT); + if (err) return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); if (apps_name.length > 0 && action != 'start') Common.printOut(cst.PREFIX_MSG_WARNING + 'Applications %s not running, starting...', apps_name.join(', ')); // Start missing apps @@ -507,7 +507,7 @@ CLI.actionFromJson = function(action, file, opts, jsonVia, cb) { data = fs.readFileSync(file); } catch(e) { Common.printError(cst.PREFIX_MSG_ERR + 'File ' + file +' not found'); - return cb ? cb(e) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(e)) : Common.exitCli(cst.ERROR_EXIT); } try { @@ -515,7 +515,7 @@ CLI.actionFromJson = function(action, file, opts, jsonVia, cb) { } catch(e) { Common.printError(cst.PREFIX_MSG_ERR + 'File ' + file + ' malformated'); console.error(e); - return cb ? cb(e) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(e)) : Common.exitCli(cst.ERROR_EXIT); } } else if (jsonVia == 'pipe') { appConf = Utility.parseConfig(file, 'pipe'); @@ -606,12 +606,12 @@ CLI.startup = function(platform, opts, cb) { if (opts.user) { console.log(cst.PREFIX_MSG + 'You have to run this command as root. Execute the following command:'); console.log(chalk.grey(' sudo su -c "env PATH=$PATH:' + p.dirname(process.execPath) + ' pm2 startup ' + platform + ' -u ' + opts.user + ' --hp ' + process.env.HOME + '"')); - return cb ? cb({msg: 'You have to run this with elevated rights'}) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr('You have to run this with elevated rights')) : Common.exitCli(cst.ERROR_EXIT); } return exec('whoami', function(err, stdout, stderr) { console.log(cst.PREFIX_MSG + 'You have to run this command as root. Execute the following command:'); console.log(chalk.grey(' sudo su -c "env PATH=$PATH:' + p.dirname(process.execPath) + ' pm2 startup ' + platform + ' -u ' + stdout.trim() + ' --hp ' + process.env.HOME + '"')); - return cb ? cb({msg: 'You have to run this with elevated rights'}) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr('You have to run this with elevated rights')) : Common.exitCli(cst.ERROR_EXIT); }); } @@ -658,7 +658,7 @@ CLI.startup = function(platform, opts, cb) { if (!fs.existsSync(scriptFile)) { Common.printOut(script); Common.printOut(cst.PREFIX_MSG_ERR + ' Script ' + scriptFile + ' does not exists'); - return cb ? cb(new Error('Script does not exists') : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(new Error('Script does not exists')) : Common.exitCli(cst.ERROR_EXIT); } var cmd; @@ -711,7 +711,7 @@ CLI.startup = function(platform, opts, cb) { if (err) { Common.printError(err); Common.printError('----- Are you sure you use the right platform command line option ? centos / redhat, amazon, ubuntu, gentoo, systemd or darwin?'); - return cb ? cb(new Error(err)) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); } Common.printOut(stde.toString().replace(/[\r\n]$/, '')); Common.printOut(stdo.toString().replace(/[\r\n]$/, '')); @@ -725,13 +725,13 @@ CLI.logrotate = function(opts, cb) { return exec('whoami', function(err, stdout, stderr) { Common.printError(cst.PREFIX_MSG + 'You have to run this command as root. Execute the following command:\n' + chalk.grey(' sudo env PATH=$PATH:' + p.dirname(process.execPath) + ' pm2 logrotate -u ' + stdout.trim())); - cb ? cb({msg: 'You have to run this with elevated rights'}) : Common.exitCli(cst.ERROR_EXIT); + cb ? cb(retErr('You have to run this with elevated rights')) : Common.exitCli(cst.ERROR_EXIT); }); } if(!fs.existsSync('/etc/logrotate.d')) { Common.printError(cst.PREFIX_MSG + '/etc/logrotate.d does not exist we can not copy the default configuration.'); - return cb ? cb({msg: '/etc/logrotate.d does not exist'}) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr('/etc/logrotate.d does not exist')) : Common.exitCli(cst.ERROR_EXIT); } var script = fs.readFileSync(path.join(__dirname, cst.LOGROTATE_SCRIPT), {encoding: 'utf8'}); @@ -779,7 +779,7 @@ CLI.reset = function(process_name, cb) { return next(); }); }, function(err) { - if (err) return cb(new Error(err)); + if (err) return cb(retErr(err)); return cb ? cb(null, {success:true}) : speedList(); }); } @@ -788,7 +788,7 @@ CLI.reset = function(process_name, cb) { Common.getAllProcessId(function(err, ids) { if (err) { Common.printError(err); - return cb ? cb(new Error(err)) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); } return processIds(ids, cb); }); @@ -797,7 +797,7 @@ CLI.reset = function(process_name, cb) { Common.getProcessIdByName(process_name, function(err, ids) { if (err) { Common.printError(err); - return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); } if (ids.length === 0) { Common.printError('Unknown process name'); @@ -824,7 +824,7 @@ CLI.dump = function(cb) { Satan.executeRemote('getMonitorData', {}, function(err, list) { if (err) { Common.printError('Error retrieving process list: ' + err); - return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); } /** @@ -872,7 +872,7 @@ CLI.resurrect = function(cb) { apps = fs.readFileSync(cst.DUMP_FILE_PATH); } catch(e) { Common.printError(cst.PREFIX_MSG_ERR + 'No processes saved; DUMP file doesn\'t exist'); - if (cb) return cb(e); + if (cb) return cb(retErr(e)); else return Common.exitCli(cst.ERROR_EXIT); } @@ -938,7 +938,7 @@ CLI.web = function(cb) { }, function(err, proc) { if (err) { Common.printError(cst.PREFIX_MSG_ERR + 'Error while launching application', err.stack || err); - return cb ? cb({msg:err}) : speedList(); + return cb ? cb(retErr(err)) : speedList(); } Common.printOut(cst.PREFIX_MSG + 'Process launched'); return cb ? cb(null, proc) : speedList(); @@ -1089,7 +1089,7 @@ CLI._operate = function(action_name, process_name, envs, cb) { return next(); }); }, function(err) { - if (err) return cb ? cb(new Error(err)) : Common.exitCli(cst.ERROR_EXIT); + if (err) return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); return cb ? cb(null, ret) : speedList(); }); } @@ -1098,7 +1098,7 @@ CLI._operate = function(action_name, process_name, envs, cb) { Common.getAllProcessId(function(err, ids) { if (err) { Common.printError(err); - return cb ? cb(err) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); } if (!ids || ids.length === 0) { Common.printError(cst.PREFIX_MSG_WARNING + 'No process found'); @@ -1119,7 +1119,7 @@ CLI._operate = function(action_name, process_name, envs, cb) { Common.getProcessIdByName(process_name, allow_module_restart, function(err, ids) { if (err) { Common.printError(err); - return cb ? cb(err) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); } if (!ids || ids.length === 0) { Common.printError(cst.PREFIX_MSG_ERR + 'Process %s not found', process_name); @@ -1257,7 +1257,7 @@ CLI.getProcessIdByName = function(name, cb) { Common.getProcessIdByName(name, function(err, id) { if (err) { Common.printError(err); - return cb ? cb(err) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); } if (!cb) console.log(id); return cb ? cb(null, id) : Common.exitCli(cst.SUCCESS_EXIT); @@ -1298,7 +1298,7 @@ CLI.list = function(opts, cb) { Satan.executeRemote('getMonitorData', {}, function(err, list) { if (err) { Common.printError(err); - return cb ? cb(err) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); } if (opts && opts.rawArgs && opts.rawArgs.indexOf('--watch') > -1) { @@ -1377,7 +1377,7 @@ CLI.scale = function(app_name, number, cb) { Common.getProcessByName(app_name, function(err, procs) { if (err) { Common.printError(err); - return cb ? cb({msg:err}) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); } if (!procs || procs.length === 0) { @@ -1428,7 +1428,7 @@ CLI.flush = function(cb) { Satan.executeRemote('getMonitorData', {}, function(err, list) { if (err) { Common.printError(err); - return cb ? cb(err) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); } list.forEach(function(l) { Common.printOut(cst.PREFIX_MSG + 'Flushing'); @@ -1495,7 +1495,7 @@ CLI.reloadLogs = function(cb) { Satan.executeRemote('reloadLogs', {}, function(err, logs) { if (err) { Common.printError(err); - return cb ? cb(err) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); } Common.printOut('All logs reloaded'); return cb ? cb(null, logs) : Common.exitCli(cst.SUCCESS_EXIT); @@ -1515,7 +1515,7 @@ CLI.sendDataToProcessId = function(proc_id, packet, cb) { Satan.executeRemote('sendDataToProcessId', packet, function(err, res) { if (err) { Common.printError(err); - return cb ? cb(new Error(err)) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); } Common.printOut('successfully sent data to process'); return cb ? cb(null, res) : speedList(); @@ -1536,7 +1536,7 @@ CLI.sendSignalToProcessName = function(signal, process_name, cb) { }, function(err, list) { if (err) { Common.printError(err); - return cb ? cb(err) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); } Common.printOut('successfully sent signal %s to process name %s', signal, process_name); return cb ? cb(null, list) : speedList(); @@ -1557,7 +1557,7 @@ CLI.sendSignalToProcessId = function(signal, process_id, cb) { }, function(err, list) { if (err) { Common.printError(err); - return cb ? cb(err) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); } Common.printOut('successfully sent signal %s to process id %s', signal, process_id); return cb ? cb(null, list) : speedList(); @@ -1732,7 +1732,7 @@ CLI.killDaemon = CLI.kill = function(cb) { CLI.install = function(module_name, cb) { Modularizer.install(module_name, function(err, data) { if (err) - return cb ? cb(err) : speedList(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : speedList(cst.ERROR_EXIT); return cb ? cb(null, data) : speedList(cst.SUCCESS_EXIT); }); }; @@ -1743,7 +1743,7 @@ CLI.install = function(module_name, cb) { CLI.uninstall = function(module_name, cb) { Modularizer.uninstall(module_name, function(err, data) { if (err) - return cb ? cb(err) : speedList(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : speedList(cst.ERROR_EXIT); return cb ? cb(null, data) : speedList(cst.SUCCESS_EXIT); }); }; @@ -1754,7 +1754,7 @@ CLI.uninstall = function(module_name, cb) { CLI.publish = function(module_name, cb) { Modularizer.publish(function(err, data) { if (err) - return cb ? cb(err) : speedList(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : speedList(cst.ERROR_EXIT); return cb ? cb(null, data) : speedList(cst.SUCCESS_EXIT); }); }; @@ -1765,7 +1765,7 @@ CLI.publish = function(module_name, cb) { CLI.generateModuleSample = function(app_name, cb) { Modularizer.generateSample(app_name, function(err, data) { if (err) - return cb ? cb(err) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); return cb ? cb(null, data) : Common.exitCli(cst.SUCCESS_EXIT); }); }; @@ -1786,7 +1786,7 @@ CLI.deleteModule = function(module_name, cb) { Common.getAllProcess(function(err, procs) { if (err) { Common.Common.printError('Error retrieving process list: ' + err); - return cb(err); + return cb(retErr(err)); } procs.forEach(function(proc) { @@ -1799,7 +1799,7 @@ CLI.deleteModule = function(module_name, cb) { return cb(); CLI._operate('deleteProcessId', found_proc[0], function(err) { - if (err) return cb(err); + if (err) return cb(retErr(err)); Common.printOut('In memory process deleted'); return cb(); }); @@ -1827,7 +1827,7 @@ CLI.get = function(key, cb) { if (!key || key == 'all') { displayConf(function(err, data) { if (err) - return cb ? cb({success:false, err:err}) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); return cb ? cb(null, {success:true}) : Common.exitCli(cst.SUCCESS_EXIT); }); return false; @@ -1835,7 +1835,7 @@ CLI.get = function(key, cb) { Configuration.get(key, function(err, data) { if (err) { console.error(err); - return cb ? cb({success:false, err:err}) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); } // pm2 conf module-name if (key.indexOf(':') === -1 && key.indexOf('.') === -1) { @@ -1873,7 +1873,7 @@ CLI.set = function(key, value, cb) { value = Password.generate(value); Configuration.set(key, value, function(err) { if (err) - return cb ? cb({success:false, err:err }) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); InteractorDaemonizer.launchRPC(function(err) { if (err) { displayConf('pm2', function() { @@ -1899,7 +1899,7 @@ CLI.set = function(key, value, cb) { */ Configuration.set(key, value, function(err) { if (err) - return cb ? cb({success:false, err:err }) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); var values = []; @@ -1969,7 +1969,7 @@ CLI.multiset = function(serial, cb) { CLI.unset = function(key, cb) { Configuration.unset(key, function(err) { if (err) { - return cb ? cb({success:false, err:err }) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); } displayConf(function() { @@ -1988,7 +1988,7 @@ CLI.conf = function(key, value, cb) { if (key && value) { CLI.set(key, value, function(err) { if (err) - return cb ? cb({success:false, err:err}) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); return cb ? cb(null, {success:true}) : Common.exitCli(cst.SUCCESS_EXIT); }); } @@ -1996,14 +1996,14 @@ CLI.conf = function(key, value, cb) { else if (key) { CLI.get(key, function(err, data) { if (err) - return cb ? cb({success:false, err:err}) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); return cb ? cb(null, {success:true}) : Common.exitCli(cst.SUCCESS_EXIT); }); } else { displayConf(function(err, data) { if (err) - return cb ? cb({success:false, err:err}) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); return cb ? cb(null, {success:true}) : Common.exitCli(cst.SUCCESS_EXIT); }); } @@ -2080,7 +2080,7 @@ CLI.interact = function(secret_key, public_key, machine_name, recycle, cb) { recycle : recycle || null }, function(err, dt) { if (err) - return cb ? cb(err) : Common.exitCli(cst.ERROR_EXIT); + return cb ? cb(retErr(err)) : Common.exitCli(cst.ERROR_EXIT); return cb ? cb(null, dt) : Common.exitCli(cst.SUCCESS_EXIT); }); }; @@ -2091,7 +2091,7 @@ CLI.interact = function(secret_key, public_key, machine_name, recycle, cb) { */ CLI.killInteract = function(cb) { InteractorDaemonizer.killDaemon(function(err) { - return cb ? cb({msg:'Interactor not launched'}) : Common.exitCli(cst.SUCCESS_EXIT); + return cb ? cb(retErr('Interactor not launched')) : Common.exitCli(cst.SUCCESS_EXIT); }); }; @@ -2407,6 +2407,14 @@ function prepareAppName(conf){ } } +function retErr(e) { + if (!e) + return new Error('Unidentified error'); + if (e instanceof Error) + return e; + return new Error(e); +} + /** * Show warnings * @param {String} warning diff --git a/test/programmatic/return.mocha.js b/test/programmatic/return.mocha.js index 215a98ca..f99d4093 100644 --- a/test/programmatic/return.mocha.js +++ b/test/programmatic/return.mocha.js @@ -6,6 +6,7 @@ describe('Return messages checks', function() { it('should display error when forgot to connect', function(done) { pm2.list(function(err, list) { should.exist(err); + should(err).instanceof(Error); should.not.exist(list); done(); });