From 4dd916997e353dd6d68d21ab80f5551b1e257bab Mon Sep 17 00:00:00 2001 From: velvettear Date: Thu, 24 Feb 2022 11:51:21 +0100 Subject: [PATCH] cleaned up some more code --- libs/server.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/libs/server.js b/libs/server.js index 3901b9c..e0de483 100644 --- a/libs/server.js +++ b/libs/server.js @@ -64,14 +64,7 @@ function handleRequests() { // POST '/poweroff' app.post('/' + MODE_POWEROFF, (request, response) => { logger.http(request); - blinkstick.powerOff(parser.parseRequest(request.body, MODE_POWEROFF)) - .then((result) => { - response.end(JSON.stringify(result)); - }) - .catch((err) => { - response.status(500); - response.end(JSON.stringify({status: 'error', error: err.message})); - }); + handlePowerOff(parser.parseRequest(request.body, MODE_POWEROFF), response); }); } @@ -79,8 +72,9 @@ async function handleSimpleAnimation(config, response) { try { response.end(JSON.stringify(await blinkstick.simple(config))); } catch (err) { + logger.error(err); response.status(500); - response.end({status: 'error', error: err}); + response.end(JSON.stringify({status: 'error', error: err.message})); } } @@ -99,8 +93,18 @@ async function handleComplexAnimation(config, response) { if (response === undefined) { return; } + logger.error(err); response.status(500); - response.end({status: 'error', error: err}); + response.end(JSON.stringify({status: 'error', error: err.message})); + } +} + +async function handlePowerOff(config, response) { + try { + response.end(JSON.stringify(await blinkstick.powerOff(config))); + } catch (err) { + response.status(500); + response.end(JSON.stringify({status: 'error', error: err.message})); } }