Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/segment-boneyard/errors
- Owner: segment-boneyard
- Created: 2013-10-08T00:21:16.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2014-01-22T17:36:18.000Z (almost 11 years ago)
- Last Synced: 2024-04-09T16:31:03.031Z (7 months ago)
- Language: JavaScript
- Size: 180 KB
- Stars: 13
- Watchers: 39
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
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.jsvar 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.