A module for gracefully handling server errors.

npm install error-handler
19 downloads in the last day
98 downloads in the last week
218 downloads in the last month


error-handler is a Node.js module for handling server errors in a graceful manner.


$ npm install error-handler


Honestly, there's not much to it. Feel free to skip to the example.

ErrorHandler( request, response [, options] )

Handle errors relating to the current request and response. options is an object with the following fields, all optional:

handler: a function to handle the error. The function must take as parameters request, response, error, and must end the response within 1000 ms. If no handler is provided, defaults to sending a plain "Server error." message. That's ugly. You really don't want to leave this one out.

log: something to log the error with; must be able to take {error: theError}, theError.stack as parameters. bunyan works; others might, too, or you can roll your own. Defaults to console.error.

fatal: boolean. Whether or not to exit the process in which the error occured, once the response has been sent. Uses exit code 1 (failure). Defaults to true.


Here's a simple example using Node's Cluster module to restart worker processes on exit. This example is included in the example folder.

var handleErrors = require('error-handler'),
    http = require('http'),
    bunyan = require('bunyan'),
    cluster = require('cluster');

// NOTE: in real life, you'd probably want to put the master and worker
// scripts in separate files.

if (cluster.isMaster) {

  // Spawn a worker.

  // We'll use bunyan for logging.
  var logger = bunyan.createLogger({
    name: 'example',
    level: 'trace',
    master: true

  // Spawn another worker when one dies.
  cluster.on('exit', function (worker, code, signal) {
    logger.warn('Worker ' + + ' died.');


} else if (cluster.isWorker) {

  var logger = bunyan.createLogger({
    name: 'example',
    level: 'trace'

  // The error handling function. Maybe render or send an error page
  // in here. At any rate, please do better than this.
  function errorHandler (req, res, err) {
    res.writeHead(500, {'Content-Type': 'text/html'});
      '<html>' +
        '<head><title>Error</title></head>' +
        '<body><p>' + err.stack + '</p></body>' +

  var server = http.createServer(function (req, res) {

    // This takes care of errors. You'll want to call this ASAP.
    handleErrors(req, res, {
      log: logger.error.bind(logger),
      handler: errorHandler      

    // Put your application logic here.

    // For illustration purposes only.
    throw new Error('Some error from something.');

  server.listen(3000, function () {'Worker ' + + ' listening.');



npm loves you