-
Notifications
You must be signed in to change notification settings - Fork 456
Error handling mechanics
There are several ways in which an error may occur, and depending on how, it will be handled differently.
For example, if a synchronous error occurs in middleware or a route:
router.get('/', function (req, res) {
res.json(req.undefinedProperty.property);
});req.undefinedProperty is undefined, so attempting to read property on it will throw an error. This sort of synchronous error will be handled by express and result in returning an error to the request via next(error).
However, if an error is thrown asynchronously, as in the following (contrived) example:
router.get('/', function (req, res) {
setImmediate(function () {
res.json(req.undefinedProperty.property);
});
});It will result in an uncaughtException.
There are two ways in which uncaughtException is handled in Kraken.
Downstream of the shutdown middleware — which is the first middleware in the stack — any uncaughtException will be caught by the shutdown middleware and handled by either:
- The
uncaughtExceptionoption passed toshutdownmiddleware. - The default
shutdownbehavior, which is to log to console the error, callnext(error), and initiate a graceful shutdown.
An uncaughtException occurring outside of a request, such as during startup, will be handled by endgame module. endgame will either:
- Use the
uncaughtExceptionhandler passed in kraken options. - By default log to console and exit process.