I've been working on my sails project and it's a monster one you can, it's a real big project consisting models, services, controllers etc etc.
Now suddenly situation arise that the application starts trigger error
Can't set headers after they are sent
.
I do R&D on it and came to know that this is happening because of the logical error in my code, and then suddenly error change to Undefined is not a functino
and the application starts getting crash on different points due to different bug in the code.
so i starts thinking there must be some sort of methods in sailsjs which prevent application to crash so that every user of my application shouldn't loose their sessions, and i could log every type of error and and prevent application to crash
So i did R&D on it again and found following code to be written in my config/express.js
the code is something like.
process.on('uncaughtException', function (err) {
console.log(err);
})
Now here comes some questions on my mind after doing so much R&D and reading different blogs and answers on SO as well.
1) is this good approach to use this function in middleway?
2) will it unstable my application ?
3) if not then what should i do to handle every type of error in my application so that the application don't crash.
I'm using sailsjs version 0.9.13
, my package.json is as follow
{
"name": "",
"private": true,
"version": "0.0.0",
"description": "a Sails application",
"dependencies": {
"MD5": "1.2.1",
"async": "^0.9.0",
"autocomplete": "0.0.1",
"aws-sdk": "^2.1.39",
"bitpay-node": "0.0.4",
"convert-json": "^0.4.0",
"cron": "^1.0.5",
"download": "^0.1.18",
"ejs": "0.8.4",
"emailjs": "^0.3.8",
"express": "^4.9.8",
"feed": "0.2.6",
"fs": "0.0.2",
"grunt": "0.4.1",
"ipv6": "3.1.1",
"js-combinatorics": "^0.4.0",
"mkdirp": "^0.5.0",
"moment": "^2.9.0",
"mysql": "2.2.0",
"nodemailer": "0.6.3",
"optimist": "0.3.4",
"pagination": "^0.4.3",
"payment-paypal-payflowpro": "0.0.4",
"paynode": "^0.3.6",
"paypal-rest-sdk": "^1.0.0",
"pdfkit": "0.6.2",
"request": "2.34.0",
"request-json": "0.4.10",
"sails": "0.9.13",
"sails-disk": "~0.9.0",
"sequelize": "1.7.3",
"url": "^0.10.3",
"wkhtmltopdf": "^0.1.4",
"xlsjs": "^0.7.1",
"xml2js": "^0.4.9"
},
"scripts": {
"start": "node app.js",
"debug": "node debug app.js"
},
"main": "app.js",
"repository": "",
"author": "",
"license": ""
}
I've also checked the error handling of express.js on http://expressjs.com/guide/error-handling.html but I'm not sure how to use it in sailsjs.
Please guide me which approach is best to handle errors in sailsjs. Thanks
The best is to fix your bugs, whatever you use to handle errors globally (uncaughtException) will make your application unstable.
Sails doesn't provide any error handler except the one comes with express or node.js but like the doc said : http://sailsjs.org/documentation/concepts/deployment#/deploy
You can use forever.js to restart automaticaly your server when it crash. And for your sessions problem you can store sessions into redis or mongoDB by editing config/session.js
file of your sails project, like this sessions was not lost when sails restart.
You can use try/catch
or node domains to handle error on some explicit code but handle errors globally it's not safe at all and it's not a good practice.
Look this question/answer for more information about error handling in node.js