From c13c8fdc64d5e64e732375e3029b49d6b4ff3b08 Mon Sep 17 00:00:00 2001 From: Drew Short Date: Thu, 9 Jan 2020 21:25:50 -0600 Subject: [PATCH] Additional error handling and code cleanup --- README.md | 5 +++-- bot/bot.js | 28 ++++++++++++++-------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 71879dd..dca3cb7 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ npm install ### Requirements: -Nodejs 12 LTS +NodeJS 12 LTS OR @@ -32,11 +32,12 @@ Docker ```bash npm install # -./entrypoint.sh run +./scripts/run_development_local.sh ``` #### Docker: ```bash +# ./scripts/run_development_docker.sh ``` diff --git a/bot/bot.js b/bot/bot.js index a16e93b..0e3e4e3 100644 --- a/bot/bot.js +++ b/bot/bot.js @@ -71,6 +71,16 @@ class Bot { }); } + let attemptPasswordLogin = (botClient, botConfig) => { + return botClient.loginWithPassword(botConfig.userId, botConfig.userPassword) + .then((data) => { + logger.info("Successfully authenticated with password %o", data); + return startServerConnection(); + }, (err) => { + logger.error("Failed to authenticate with password %o", err); + }); + } + logger.info("Authenticating With Server"); if (typeof botConfig.accessToken !== 'undefined') { await botClient.loginWithToken(this.config.accessToken) @@ -80,25 +90,13 @@ class Bot { }, (err) => { logger.error("Failed to authenticate with access token %o", err); if (typeof botConfig.userPassword !== 'undefined') { - return botClient.loginWithPassword(botConfig.userId, botConfig.userPassword) - .then((data) => { - logger.info("Successfully authenticated with password %o", data); - return startServerConnection(); - }, (err) => { - logger.error("Failed to authenticate with password %o", err); - }); + return attemptPasswordLogin(botClient, botConfig); } else { logger.error("No fallback password provided!"); } }); } else if (typeof botConfig.userPassword !== 'undefined') { - await botClient.loginWithPassword(botConfig.userId, botConfig.userPassword) - .then((data) => { - logger.info("Successfully authenticated with password %o", data); - return startServerConnection(); - }, (err) => { - logger.error("Failed to authenticate with password %o", err); - }); + await attemptPasswordLogin(botClient, botConfig); } else { logger.error("No authentication credentials available!"); process.exit(1); @@ -169,6 +167,8 @@ class Bot { roomId, message.createBasic("Shutting down", message.types.NOTICE) ).then(() => { logger.debug("Notified %s of shutdown", roomId); + }, (err) => { + logger.error("Unable to send message to room %s because %s", roomId, err.errcode); })); }); } else {