Skip to content

Commit

Permalink
improv: standard and remove useless try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
Eywek committed Aug 22, 2018
1 parent 6359e8a commit e2a5806
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
15 changes: 4 additions & 11 deletions src/InteractorClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ module.exports = class InteractorDaemonizer {
return process.exit(1)
}
})
}
else {
} else {
console.error('unexpected error')
console.error(e)
}
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 10 additions & 8 deletions src/InteractorDaemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit e2a5806

Please sign in to comment.