-
-
Save gplv2/12ad615a1f34f9bc77921fb232e4e922 to your computer and use it in GitHub Desktop.
NodeJS Error Handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.on | |
( | |
'uncaughtException', | |
function (err) | |
{ | |
var stack = err.stack; | |
var timeout = 1; | |
// print note to logger | |
logger.log("SERVER CRASHED!"); | |
// logger.printLastLogs(); | |
logger.log(err, stack); | |
// save log to timestamped logfile | |
// var filename = "crash_" + _2.formatDate(new Date()) + ".log"; | |
// logger.log("LOGGING ERROR TO "+filename); | |
// var fs = require('fs'); | |
// fs.writeFile('logs/'+filename, log); | |
// email log to developer | |
if(helper.Config.get('email_on_error') == 'true') | |
{ | |
logger.log("EMAILING ERROR"); | |
require('./Mailer'); // this is a simple wrapper around nodemailer http://documentup.com/andris9/nodemailer/ | |
helper.Mailer.sendMail("GAMEHUB NODE SERVER CRASHED", stack); | |
timeout = 10; | |
} | |
// Send signal to clients | |
// logger.log("EMITTING SERVER DOWN CODE"); | |
// helper.IO.emit(SIGNALS.SERVER.DOWN, "The server has crashed unexpectedly. Restarting in 10s.."); | |
// If we exit straight away, the write log and send email operations wont have time to run | |
setTimeout | |
( | |
function() | |
{ | |
logger.log("KILLING PROCESS"); | |
process.exit(); | |
}, | |
// timeout * 1000 | |
timeout * 100000 // extra time. pm2 auto-restarts on crash... | |
); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment