Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/segment-boneyard/errors

Simple abstraction to handle custom errors in your codebase.
https://github.com/segment-boneyard/errors

Last synced: about 5 hours ago
JSON representation

Simple abstraction to handle custom errors in your codebase.

Awesome Lists containing this project

README

        

# errors

Simple abstraction to handle custom errors in your codebase.

[![Build Status](https://travis-ci.org/segmentio/errors.png?branch=master)](https://travis-ci.org/segmentio/errors)

## Installation

$npm install segmentio/errors
$component install segmentio/errors

## Example

```js
var RandomError = require('./random-error');
var errors = require('errors')();

/**
* Add `Random` error to our known errors map.
*/

errors.add('Random', RandomError);

/**
* Wrap any errors the map recognizes after getting from Mongo.
*/

function get (id, callback) {
mongo.findById(id, function (err, res) {
callback(errors.wrap(err), res);
});
}
```

```js
// random-error.js

var inherit = require('util').inherits;

/**
* Random Error.
*/

function RandomError (err) {
Error.call(this);
Error.captureStackTrace(this, arguments.callee);
}

inherit(RandomError, Error);

/**
* Check if `err` is a `RandomError`.
*/

RandomError.is = function (err) {
return err && err.code == 1042;
};
```

## API

### errors()

Initialize a new error map.

### .Errors

The map of error constructors, so you can expose it so external APIs can do things like check `instanceof`.

### #add(name, Constructor)

Add a new error `Constructor` to the map with `name`. The `Constructor` must have a function to check whether the error is its type exposed as `.is`.

### #match(error)

Check with the `error` matches any of the rules of the custom constructors.

### #wrap(error)

Wrap an `error` in its appropriate custom constructor.