From e2a58060d5bf68ffe47e36f6c867a14d80d656ce Mon Sep 17 00:00:00 2001 From: Eywek Date: Wed, 22 Aug 2018 10:52:42 +0200 Subject: [PATCH] improv: standard and remove useless try catch --- src/InteractorClient.js | 15 ++++----------- src/InteractorDaemon.js | 18 ++++++++++-------- test.js | 8 ++++++++ 3 files changed, 22 insertions(+), 19 deletions(-) create mode 100644 test.js diff --git a/src/InteractorClient.js b/src/InteractorClient.js index efb8201..cca33da 100644 --- a/src/InteractorClient.js +++ b/src/InteractorClient.js @@ -60,8 +60,7 @@ module.exports = class InteractorDaemonizer { return process.exit(1) } }) - } - else { + } else { console.error('unexpected error') console.error(e) } @@ -197,17 +196,11 @@ module.exports = class InteractorDaemonizer { stdio: ['ipc', out, err] }) - var prevPid = null try { - prevPid = fs.readFileSync(constants.INTERACTOR_PID_PATH) + let prevPid = fs.readFileSync(constants.INTERACTOR_PID_PATH) prevPid = parseInt(prevPid) - } catch(e) { - } - - if (prevPid) { - try { - process.kill(prevPid) - }catch(e){} + process.kill(prevPid) + } catch (e) { } fs.writeFileSync(cst.INTERACTOR_PID_PATH, child.pid) diff --git a/src/InteractorDaemon.js b/src/InteractorDaemon.js index 0190a46..377a5df 100644 --- a/src/InteractorDaemon.js +++ b/src/InteractorDaemon.js @@ -43,13 +43,16 @@ const InteractorDaemon = module.exports = class InteractorDaemon { this._internalDebugger() } - sendToParent(data) { - if (!process.connected || !process.send) - return console.log('Could not send data to parent') + /** + * Use process.send() if connected + * @param {Object} data + */ + sendToParent (data) { + if (!process.connected || !process.send) return console.log('Could not send data to parent') try { process.send(data) - } catch(e) { + } catch (e) { console.trace('Parent process disconnected') } } @@ -270,9 +273,8 @@ const InteractorDaemon = module.exports = class InteractorDaemon { return cb(null, data) } - if (!data.endpoints || data.active == false) { - console.trace(data) - return cb(new Error('Endpoints field not present or not active')) + if (!data.endpoints || data.active === false) { + return cb(new Error(`Endpoints field not present or not active (${JSON.stringify(data)})`)) } this.DAEMON_ACTIVE = true @@ -414,7 +416,7 @@ if (require.main === module) { console.log(`[PM2 Agent] Using (Public key: ${infos.public_key}) (Private key: ${infos.secret_key}) (Info node: ${infos.info_node})`) // Exit anyway the errored agent - var timeout = setTimeout(function() { + var timeout = setTimeout(_ => { console.error('Daemonization of failsafe agent did not worked') daemon.exit(err) }, 2000) diff --git a/test.js b/test.js new file mode 100644 index 0000000..5d22eb2 --- /dev/null +++ b/test.js @@ -0,0 +1,8 @@ +const http = require('http') + +const server = http.createServer((req, res) => { + res.writeHead(500) + res.end(JSON.stringify({msg: 'Database error.'})) +}) + +server.listen(1337)