Sails - catch global adapter errors that crash the server

express, node.js, sails.js

Solution

from the sails docs: http://sailsjs.org/#!documentation/config.500

thats the error handling sails exposes from within the config

if your error passes that, you can hook in there, otherwise you can hook in node's process

process.on('uncaughtException', function (err) {
  if (err.toString() === 'Error spawning mySQL connection') {
    //rende some error page
  }
})

if the exception thrown is async the only way to catch it is trough process

do note however, that these kinds of errors are almost always unrecoverable, so crashing (and restarting) is the best approach

most modules loaded use local variables and expose only a subset of their internals trough `module.exports`, unloading a module and restarting its local code can be done, but you would need to unload all dependant modules and all modules holding references to it also. Thats why the normal approach is to let it crash

Problem

I'm trying to find the best place to handle connectivity errors, or any other global errors that crash the server. What is the right place to catch adapter/global errors and not have them crash the server? Specifically, I want to handle these types of errors in a graceful way: ``` Error spawning mySQL connection: error: Hook failed to load: orm (Error: connect ECONNREFUSED) error: Error encountered while loading Sails core! error: Error: connect ECONNREFUSED ```

Original source

Related problems