# error-handler

> A module for gracefully handling server errors.

Latest version **1.0.0** (published 2014-05-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install error-handler
pnpm add error-handler
yarn add error-handler
bun add error-handler
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2014-05-10 |
| First published | 2014-02-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Luc Thevenard |
| Maintainers | lucthev |
| Keywords | error, handler, domain |

## Links

- npm: https://www.npmjs.com/package/error-handler
- Repository: https://github.com/lucthev/error-handler
- Issues: https://github.com/lucthev/error-handler/issues
- npm.io page: https://npm.io/package/error-handler

## Alternatives

- [@sentry/react-native](https://npm.io/package/@sentry/react-native.md) — 2.6M weekly downloads
- [@ardatan/aggregate-error](https://npm.io/package/@ardatan/aggregate-error.md) — 708.1K weekly downloads
- [custom-error-generator](https://npm.io/package/custom-error-generator.md) — 2.0K weekly downloads
- [@technik-sde/prosemirror-recreate-transform](https://npm.io/package/@technik-sde/prosemirror-recreate-transform.md) — 1.5K weekly downloads
- [@suchipi/error-utils](https://npm.io/package/@suchipi/error-utils.md) — 78 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2014-05-10
- 0.1.4 — 2014-03-09
- 0.1.3 — 2014-03-09
- 0.1.2 — 2014-02-20
- 0.1.1 — 2014-02-17
- 0.1.0 — 2014-02-11
- 0.0.2 — 2014-02-10
- 0.0.1 — 2014-02-08

## README

# error-handler

error-handler is a [Node.js](http://nodejs.org) 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](https://github.com/trentm/node-bunyan) for logging and Node's [Cluster](http://nodejs.org/api/cluster.html) module to restart worker processes on exit. This example is included in the [example](https://github.com/lucthev/error-handler/tree/master/example) folder.

```javascript
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](https://github.com/lucthev/error-handler/blob/master/LICENSE.txt)

---
_Source: https://npm.io/package/error-handler · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
