From ed68ff4a34ce7c4d6d3b931420df65495d420979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Wed, 23 Aug 2017 01:42:59 -0400 Subject: [PATCH] Make sure CLI does not crash on `undefined.length` when we figured out users could not be read already --- src/clientManager.js | 8 +++++++- src/command-line/add.js | 4 ++++ src/command-line/edit.js | 5 +++++ src/command-line/list.js | 5 +++++ src/command-line/remove.js | 15 ++++++++++----- src/command-line/reset.js | 5 +++++ 6 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/clientManager.js b/src/clientManager.js index ba56d6c1..c076494f 100644 --- a/src/clientManager.js +++ b/src/clientManager.js @@ -35,6 +35,12 @@ ClientManager.prototype.autoloadUsers = function() { const users = this.getUsers(); const noUsersWarning = `There are currently no users. Create one with ${colors.bold("lounge add ")}.`; + // There was an error, already logged, but we have to crash the server as + // user directory could not be accessed + if (users === undefined) { + process.exit(1); + } + if (!users.length) { log.info(noUsersWarning); } @@ -91,7 +97,7 @@ ClientManager.prototype.getUsers = function() { } }); } catch (e) { - log.error("Failed to get users", e); + log.error(`Failed to get users (${e})`); return; } return users; diff --git a/src/command-line/add.js b/src/command-line/add.js index 198e6f4c..c88833d5 100644 --- a/src/command-line/add.js +++ b/src/command-line/add.js @@ -18,6 +18,10 @@ program const manager = new ClientManager(); const users = manager.getUsers(); + if (users === undefined) { // There was an error, already logged + return; + } + if (users.indexOf(name) !== -1) { log.error(`User ${colors.bold(name)} already exists.`); return; diff --git a/src/command-line/edit.js b/src/command-line/edit.js index eb6ec671..b2da9450 100644 --- a/src/command-line/edit.js +++ b/src/command-line/edit.js @@ -13,6 +13,11 @@ program .on("--help", Utils.extraHelp) .action(function(name) { var users = new ClientManager().getUsers(); + + if (users === undefined) { // There was an error, already logged + return; + } + if (users.indexOf(name) === -1) { log.error(`User ${colors.bold(name)} does not exist.`); return; diff --git a/src/command-line/list.js b/src/command-line/list.js index df447c94..e10a7375 100644 --- a/src/command-line/list.js +++ b/src/command-line/list.js @@ -16,6 +16,11 @@ program } var users = new ClientManager().getUsers(); + + if (users === undefined) { // There was an error, already logged + return; + } + if (!users.length) { log.info(`There are currently no users. Create one with ${colors.bold("lounge add ")}.`); } else { diff --git a/src/command-line/remove.js b/src/command-line/remove.js index e0a971bc..e15f13ff 100644 --- a/src/command-line/remove.js +++ b/src/command-line/remove.js @@ -10,10 +10,15 @@ program .description("Remove an existing user") .on("--help", Utils.extraHelp) .action(function(name) { - var manager = new ClientManager(); - if (manager.removeUser(name)) { - log.info(`User ${colors.bold(name)} removed.`); - } else { - log.error(`User ${colors.bold(name)} does not exist.`); + const manager = new ClientManager(); + + try { + if (manager.removeUser(name)) { + log.info(`User ${colors.bold(name)} removed.`); + } else { + log.error(`User ${colors.bold(name)} does not exist.`); + } + } catch (e) { + // There was an error, already logged } }); diff --git a/src/command-line/reset.js b/src/command-line/reset.js index 9546435e..6721ff4b 100644 --- a/src/command-line/reset.js +++ b/src/command-line/reset.js @@ -13,6 +13,11 @@ program .on("--help", Utils.extraHelp) .action(function(name) { var users = new ClientManager().getUsers(); + + if (users === undefined) { // There was an error, already logged + return; + } + if (users.indexOf(name) === -1) { log.error(`User ${colors.bold(name)} does not exist.`); return;