Skip to content

Commit

Permalink
Handler errors
Browse files Browse the repository at this point in the history
  • Loading branch information
shinylightdev committed Jan 14, 2021
1 parent 452514f commit bb1271a
Showing 1 changed file with 56 additions and 14 deletions.
70 changes: 56 additions & 14 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ proxyServer.on("connection", (clientToProxySocket) => {
proxyHandler.RunForEachConnection(clientToProxySocket, whiteListDomains);
});

// =============================================
// Problems with proxy server.
// =============================================
proxyServer.on("error", exitHandler.bind(null, {
exit: true, cleanup: true
}));


// =============================================
// Close connection.
Expand All @@ -34,20 +41,55 @@ proxyServer.on("close", () => {


// =============================================
// Problems with proxy server.
// Problems with this process.
// =============================================
proxyServer.on("error", (error) => {
console.log("\n");
console.log("SERVER ERROR");
console.log(error);
console.log("\n");
});
process.on("uncaughtException", exitHandler.bind(null, {
exit: true, cleanup: true }
));


// =============================================
// Problems with this process.
// =============================================
process.on("uncaughtException", (error) => {
console.error("There was an uncaught error", error);
process.exit(1);
});
// So the program will not close instantly.
process.stdin.resume();

function exitHandler(options, exitCode) {

// Cleaning up.
if (options.cleanup) {
proxyServer.close();

// CLEAN UP HERE!! kill port?
// console.log("CLEANUP");
}

if (exitCode || exitCode === 0) {
console.log(exitCode);
// console.log("EXITCODE");
}

// Exiting...
if (options.exit) {
process.exit();
}
}


// https://stackoverflow.com/questions/14031763/doing-a-cleanup-action-just-before-node-js-exits
// do something when app is closing

process.on("exit", exitHandler.bind(null, {
cleanup: true
}));

// catches ctrl+c event
process.on("SIGINT", exitHandler.bind(null, {
exit: true, cleanup: true
}));

// catches "kill pid" (for example: nodemon restart)
process.on("SIGUSR1", exitHandler.bind(null, {
exit: true
}));

process.on("SIGUSR2", exitHandler.bind(null, {
exit: true
}));

0 comments on commit bb1271a

Please sign in to comment.