I just started trying out SailsJS a few days ago.
I've realized that the Node is terminated whenever I have an uncaught exception.
I have a list of controllers and each of them calls a specific service JS file (Containing logics and DB calls) in services/.
Can I write a global error handler for all services so that any type of error that occurs from these services should be handled by it and appropriate error response has to be communicated to front-end.
I tried using process.on('uncaughtexception') or some of basic exceptions but it needs to be added to each service method.
Also can I have one common point for all service calls made from client to server through which all io.socket.post() and io..socket.get() goes through
I would appreciate any pointer/article that would show me the common best practices for handling uncaught exceptions in SailsJS and using shorter code rather than writing redundant code in all services.
The first best practice of “Exception Handling” is, don’t overuse “Exception Handling.” Typically, we handle exceptions at the outer layers and throws from the inner so that once an exception occurs, we can better understand what leads to it. However, one of the common mistakes developers make is to overuse exception handling.
And, one such fundamental practice is to deal with program exceptions. So, in this article, I will discuss different patterns and best practices around Exception Handling with JavaScript. 1. Don’t Overuse the “Try-Catch” The first best practice of “Exception Handling” is, don’t overuse “Exception Handling.”
This is the most famous principle about Exception handling. We should throw an exception as soon as we can and we should catch it as late as we can It means we should throw an exception in low level methods where we execute our business logic and make sure exception is thrown to several layers until we reach specific layer to handle it
Typically, we handle exceptions at the outer layers and throws from the inner so that once an exception occurs, we can better understand what leads to it. However, one of the common mistakes developers make is to overuse exception handling. Sometimes we do it to keep the code look consistent across different files and methods.
Best practice is using Domains in your controller. This will handle exceptions in async code, and its relatively straight forward.
You can use something like trycatch to simplify things a little, but domain based exceptions will be most effective. It'll insure that exceptions do not crash your application. Just create a new domain in your controller and run your controller methods inside of that domain.
Sailsjs being based on express you can use connect middleware, and you can seamlessly create a new domain from middleware. There such thing as express-domain-middleware. This might be the most aesthetic option, and most convenient.
Update: As mention by Benjamin Gruenbaum, Domains are planned to become deprecated in v1 of node. Perhaps you should read through Joyents Error Handling Best Practices. Its agnostic to the framework you are using.
Additonally you can still use Domains, while there isn't a way to globally handle errors in node.js otherwise. Once deprecated you could always remove your dependence on Domains, relatively easily. That said, it may be best not to rely solely on domains.
Strongloop also provides a library inspired by domains called Zone. This is also an option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With