Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/colored-coins/error-handler

Express.js error handling middleware to be used for uniformly formatting errors returned from API calls.
https://github.com/colored-coins/error-handler

Last synced: 6 days ago
JSON representation

Express.js error handling middleware to be used for uniformly formatting errors returned from API calls.

Awesome Lists containing this project

README

        

[![Build Status](https://travis-ci.org/Colored-Coins/error-handler.svg?branch=master)](https://travis-ci.org/Colored-Coins/error-handler)
[![Coverage Status](https://coveralls.io/repos/github/Colored-Coins/error-handler/badge.svg?branch=master)](https://coveralls.io/github/Colored-Coins/error-handler?branch=master)
[![npm version](https://badge.fury.io/js/cc-error-handler.svg)](https://badge.fury.io/js/cc-error-handler)
# error-handler
Express.js error handling middleware.
## Installation
```sh
$ npm install cc-error-handler
```
## Running the tests
```
$ npm install
$ mocha
```
## API
```javascript
var errorhandler = require('cc-error-handler')
```
### errorhandler(options)
Create new error handling middleware.
#### options
##### env
'development' will include stack trace, and will accumulate original errors

initiated from third party or separate servers. Default is undefined.
##### log
One of two types:

boolean - a boolean for determining whether the error handler should log the error messages. `true` will use `console.error` by default for logging.

function - a function to process an error, invoked with `err`.

Default is `true`.

## Example
As with any express error handling middleware, it should be put after the router middleware:
```javascript
var express = require('express')
var app = express()
var bodyParser = require('body-parser')
var errorHandler = require('cc-error-handler')

app.use(bodyParser())
app.get('/error', function (req, res, next) {
next('Something went wrong')
})
app.use(errorHandler())
```