Handling errors at a global level

2019-07-08 02:03发布

I am trying to understand how to build my error handling system for my api.

Let's say I have a the following line in a controller method :

  var age = json.info.age; 

with

json = {"id":1, "name":"John", info": {"age":27, "sex":"m"}}

Let's say that the object doesn't contain an info field, I'll get the following error TypeError: Cannot read property 'info' of undefined and my server will crash.

Is there a way to make a higher level abstraction and catch all the potential errors that I could have? Or should I have a try/catch system for each of the methods of my controllers?

3条回答
可以哭但决不认输i
2楼-- · 2019-07-08 02:41

So that's how I did it, thanks to Zagen's answer.

module.exports.bootstrap = function(cb) {

  process.on('uncaughtException', function (err) {
    //Handle your errors here
    logger.fatal(err);
    global.__current__.res.serverError();
  })
  cb();
};

I send a generic error 500 to the user if any uncaught exception is thrown, and I log the error to the fatal level. On that way, my server is still accessible 24/7 and I can monitor the logs at another level and trigger an alarm on a fatal error. I can then fix the exception that was thrown.

查看更多
走好不送
3楼-- · 2019-07-08 02:44

You can check this way if you want to:

if (object != null && object.response != null && object.response.docs != null){
//Do your stuff here with your document
}

I don't really get what is your "object" variable in the first place, so i don't know if you can check it at a different level, is it a sails parameter to your controller ?

查看更多
老娘就宠你
4楼-- · 2019-07-08 02:49

BEWARE OF THE CODE BELOW, IT WILL BITE YOU WHENEVER IT CAN!

Don't use the code snippet below if you do not understand its implications, please read the whole answer.


You can use the node way for uncaught errors. Add this in your config/bootstrap.js

Updated the snippet below to add what was said in the comments, also added a warning about using a global to respond to the user.

process.on('uncaughtException', function (err) {
    // Handle your errors here

    // global.__current__ is added via middleware
    // Be aware that this is a bad practice,
    // global.__current__ being a global, can change
    // without advice, so you might end responding with 
    // serverError() to a different request than the one
    // that originated the error if this one happened async 
    global.__current__.res.serverError();
  })

Now, can doesn't mean should. It really depends on your needs, but do not try to catch BUGS in your code, try to catch at a controller level the issues that might not happen every time but are somehow expected, like a third-party service that responded with empty data, you should handle that in your controller. The uncaughtException is mainly for logging purposes, its better to let your app crash if there is a bug. Or you can do something more complicated (that might be better IMHO), which is to stop receiving requests, respond to the error 500 (or a custom one) to user that requested the faulty endpoint, and try to complete the other requests that do not relate to that controller, then log and shutdown the server. You will need several instances of sails running to avoid zero downtime, but that is material for another question. What you asked is how to get uncaught exceptions at a higher lvl than the controllers.

I suggest you read the node guide for error handling

Also read about domains, even thought they are deprecated you can use them, but you would have to deal with them per controller action, since sails does not provide any help with that.

I hope it helps.

查看更多
登录 后发表回答