From d4b776333fcbca1a7c31edac4ee2055e0ec0e8de Mon Sep 17 00:00:00 2001 From: velvettear Date: Thu, 24 Feb 2022 10:41:50 +0100 Subject: [PATCH] replaced some more promises with async/await --- blinky.js | 26 +++++++++++++------------- libs/logger.js | 19 ++++++++++--------- libs/server.js | 12 ++++++------ 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/blinky.js b/blinky.js index a5dbe02..a5da2bb 100644 --- a/blinky.js +++ b/blinky.js @@ -15,17 +15,17 @@ handleInterrupts(); main(); // main - let's get this party started -function main() { - util.fileExists(global.config) - .then((result) => { - global.config = require(result.path); - global.config.path = result.path; - }) - .then(logger.initialize) - .then(server.start) - .then(logger.info) - .then(server.handleRequests) - .catch(exit); +async function main() { + try { + const config = await util.fileExists(global.config); + global.config = require(config.path); + global.config.path = config.path; + logger.initialize(); + logger.info(await server.start()); + server.handleRequests(); + } catch (err) { + exit(err); + } } // ... and it all comes crashing down @@ -38,9 +38,9 @@ function exit(err, code) { } if (err) { logger.error(err); - logger.error(packageJSON.name + " ended due to an error"); + logger.error(packageJSON.name + ' ended due to an error'); } else { - logger.info(packageJSON.name + " shutting down gracefully") + logger.info(packageJSON.name + ' shutting down gracefully') } process.exit(code); } diff --git a/libs/logger.js b/libs/logger.js index 0f89526..e442783 100644 --- a/libs/logger.js +++ b/libs/logger.js @@ -14,14 +14,11 @@ let loglevel = getLogLevel(); let timestamp = getTimestamp(); function initialize() { - return new Promise((resolve, reject) => { - if (global.config === undefined) { - reject('could not initialize logger, config is undefined'); - } - loglevel = getLogLevel(); - timestamp = getTimestamp(); - resolve(); - }); + if (global.config === undefined) { + throw new Error('could not initialize logger, config is undefined'); + } + loglevel = getLogLevel(); + timestamp = getTimestamp(); } // get the loglevel @@ -104,7 +101,11 @@ function error(message) { } return; } - trace(message.message, 'error'); + if (message.message) { + trace(message.message, 'error'); + return + } + trace(message, 'error'); } // default logging function diff --git a/libs/server.js b/libs/server.js index 5b230d2..9c80d86 100644 --- a/libs/server.js +++ b/libs/server.js @@ -23,19 +23,19 @@ const MODE_POWEROFF = require('./blinkstick.js').MODE_POWEROFF; const html = require('./index.js').get(); // run the express http server and handle gets/posts -function start() { - return new Promise(function (resolve, reject) { +async function start() { + return await new Promise(function (resolve, reject) { app.listen(config.server.port, config.server.listen) .on('listening', function () { - return resolve('server listening on ' + config.server.listen + ':' + config.server.port + '...') + resolve('server listening on ' + config.server.listen + ':' + config.server.port + '...') }) .on('error', function (err) { - return reject('error starting server (' + err + ')'); + reject('error starting server (' + err + ')'); }); }); } -async function handleRequests() { +function handleRequests() { // GET html page app.get('*', (request, response) => { logger.http(request); @@ -121,7 +121,7 @@ async function handleRequests() { }) .catch((err) => { response.status(500); - response.end({status: 'error', error: err}); + response.end(JSON.stringify({status: 'error', error: err.message})); }); }); }