2

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

Ahsan Hussain
  • 952
  • 4
  • 21
  • 42

2 Answers2

1

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

Community
  • 1
  • 1
jaumard
  • 8,202
  • 3
  • 40
  • 63
  • by globally i mean no in the controller i mean every where in the application like, like with uncaughtException in express.js i can catch every type of error in all the controller or models or services.... – Ahsan Hussain Aug 06 '15 at 12:18
  • also i asked about unvaughtException method in express.js that if it's good approach to use this method or not? also let me know if there is any type of error handling in sailsjs related to express.js – Ahsan Hussain Aug 06 '15 at 12:23
  • The right approach is to relaunch the server if it's crash with forever (on something similar). Look the sails doc http://sailsjs.org/documentation/concepts/deployment#/deploy like I said catch uncaughtException is not a good approach and can make your application unstable. There nothing in sails for errors handling except the default node solution I said above. – jaumard Aug 06 '15 at 13:38
  • ok thanks for guiding me please edit the answer so that i can upvote it. – Ahsan Hussain Aug 07 '15 at 10:47
0

While looking for a way in logging all errors from certain controllers, I came across a new way to handle errors in sails js which wasn't available in sails documentation.

We can create a file in /api/responses folder with name serverError.js

module.exports = function serverError(error) {
  sails.helpers.sendLog({
    body: { 
      url: this.req.url,
      message: `error ${error.message}`,
      stack: error.stack,
      req_body: this.req.body,
      req_headers: {
        origin: this.req.headers.origin, 
      },
      res: this.res.statusCode,
    }
  }).then(() => {});

  if( this.req.transaction) {
    this.req.transaction.end();
  } 
  const { res } = this;
  console.error(' error --> ', error);
  return res.status(500).json({
    code: 'SERVER_ERROR',
    error,
  });
};

This will handle error but will now stop the server or throw otherwise. You will have to do that manually in code.

similarly you can create a success.js file too and handle all success cases.

DHRUV GAJWA
  • 561
  • 5
  • 13