Skip to content

Error handling mechanics

Trevor edited this page Dec 31, 2015 · 2 revisions

There are several ways in which an error may occur, and depending on how, it will be handled differently.

Synchronously thrown errors in middleware / routes

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).

Asynchronously thrown errors in middleware / routes

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.

uncaughtException

There are two ways in which uncaughtException is handled in Kraken.

Uncaught for in middleware / routes

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:

  1. The uncaughtException option passed to shutdown middleware.
  2. The default shutdown behavior, which is to log to console the error, call next(error), and initiate a graceful shutdown.

Uncaught outside of a request

An uncaughtException occurring outside of a request, such as during startup, will be handled by endgame module. endgame will either:

  1. Use the uncaughtException handler passed in kraken options.
  2. By default log to console and exit process.