Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Node.js module for gracefully handling server errors.
branch: master
Failed to load latest commit information.
example Add link to the address where the example runs.
test v1.0.0
.gitignore Update test command.
.jshintrc Lint
LICENSE.txt Add License.
README.md v1.0.0
errorHandler.js v1.0.0
package.json v1.0.0

README.md

error-handler

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

Installation

$ npm install error-handler

Usage

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

ErrorHandler( request, response [, handler, notFatal] )

Handle errors relating to the current request and response.

handler is a function which is passed the relevant request, response, and error, in that order, and should probably log the error and/or send a nice error page. If no handler is given, defaults to logging the error via console.error( ) and sending a plain Server error. message. That's ugly.

By default, error-handler exits the process in which the error occured, once the error has been dealt with. If notFatal is true, this behaviour gets overridden. Note that this works as long as notFatal is truthy; so, if you want to make it a little more clear what you're doing, you could pass in a String of some kind (e.g. 'Not fatal.').

Example

Here's a simple example using bunyan for logging and 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.
  cluster.fork()

  // 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 ' + worker.id + ' died.')

    cluster.fork()
  })

} 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) {
    req.log.error({ err: err }, err.stack)

    res.writeHead(500, {'Content-Type': 'text/html'})
    res.end(
      '<html>' +
        '<head><title>Error</title></head>' +
        '<body><p style="white-space: pre">' + err.stack + '</p></body>' +
      '</html>'
    )
  }

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

    req.log = res.log = logger.child()

    // This takes care of errors. You'll want to call this ASAP.
    handleErrors(req, res, errorHandler)

    /**
     *
     * Put your application logic here.
     *
     */

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

  server.listen(3000, function () {
    logger.info('Worker ' + cluster.worker.id + ' listening.')
  })
}

License

MIT

Something went wrong with that request. Please try again.