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.

PR-URL: #135
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
Reviewed-By: Mykola Bilochub <nbelochub@gmail.com>
  • Loading branch information
lundibundi authored and belochub committed Jan 22, 2018
1 parent 2893994 commit 4e7d806
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 4e7d806

Please sign in to comment.