Skip to content

Commit

Permalink
cli: refine error messages
Browse files Browse the repository at this point in the history
* make all missing argument errors conform to single style
* report error on missing host/port, {method,event} name
  • Loading branch information
lundibundi committed May 6, 2017
1 parent 8a6bc52 commit 6037b5b
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions tools/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ rl.on('line', (line) => {
if (!processor) {
log(`Unknown command '${cmd}'`);
} else {
processor(leftover, (err, result) => {
processor(leftover.trim(), (err, result) => {
if (err) return log(`${err.name} occurred: ${err.message}`);
log(result);
});
Expand Down Expand Up @@ -202,11 +202,17 @@ function _split(str, separator, limit, leaveEmpty) {
return result;
}

const reportMissingArgument =
missing => new Error(`${missing} is not provided`);

lineProcessor.call = (tokens, callback) => {
if (tokens === undefined) {
return callback(new Error('Not enough arguments'));
return callback(reportMissingArgument('Interface name'));
}
const args = _split(tokens, ' ', 2);
if (args.length === 1) {
return callback(reportMissingArgument('Method name'));
}
let methodArgs;
try {
methodArgs = jstp.parse('[' + args[2] + ']');
Expand All @@ -222,9 +228,12 @@ lineProcessor.call = (tokens, callback) => {

lineProcessor.event = (tokens, callback) => {
if (tokens === undefined) {
return callback(new Error('Not enough arguments'));
return callback(reportMissingArgument('Interface name'));
}
const args = _split(tokens, ' ', 2);
if (args.length === 1) {
return callback(reportMissingArgument('Event name'));
}
let eventArgs;
try {
eventArgs = jstp.parse('[' + args[2] + ']');
Expand All @@ -238,14 +247,17 @@ lineProcessor.event = (tokens, callback) => {
};

lineProcessor.connect = (tokens, callback) => {
if (tokens === undefined) {
return callback(new Error('Not enough arguments'));
if (tokens === undefined || tokens.startsWith(':')) {
return callback(reportMissingArgument('Host'));
}
const args = _split(tokens, ' ', 2);
const [host, port] = _split(args[0], ':');
if (port === undefined) {
return callback(reportMissingArgument('Port'));
}
const appName = args[1];
if (appName === undefined) {
return callback(new Error('Application name is not provided'));
return callback(reportMissingArgument('Application name'));
}
commandProcessor.connect(host, port, appName, (err) => {
if (err) return callback(err);
Expand Down

0 comments on commit 6037b5b

Please sign in to comment.