https://github.com/doowb/error-base
Create custom Error classes.
https://github.com/doowb/error-base
Last synced: about 1 month ago
JSON representation
Create custom Error classes.
- Host: GitHub
- URL: https://github.com/doowb/error-base
- Owner: doowb
- License: mit
- Created: 2015-09-23T03:59:12.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-02-10T18:23:34.000Z (about 9 years ago)
- Last Synced: 2025-02-26T19:51:45.376Z (about 2 months ago)
- Language: JavaScript
- Size: 10.7 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# error-base [](https://www.npmjs.com/package/error-base) [](https://travis-ci.org/doowb/error-base)
> Create custom Error classes.
Install with [npm](https://www.npmjs.com/):
```sh
$ npm i error-base --save
```## Usage
```js
var errorBase = require('error-base');// Create a new HelperError that takes additional options.
var HelperError = errorBase('HelperError', function (msg, options) {
this.message = msg;
this.options = options || {};
});try {
// Throw a HelperError given the custom arguments
throw new HelperError('some error message', {
helper: 'helper-name',
foo: 'bar'
});
} catch (err) {
console.log(err.message);
//=> 'some error message'console.log(err.options);
//=> { helper: 'helper-name', foo: 'bar' }
}
```## API
### [errorBase](index.js#L26)
Code modified from http://stackoverflow.com/a/27925672/914583
**Params**
* `name` **{String}**: Name of new Error Class
* `init` **{Function}**: Function to call when creating new Error
* `capture` **{Boolean}**: Optional parameter to determine if the stack trace should be captured or not. (Defaults to `true`)
* `returns` **{Function}**: new Error Class**Example**
```js
var CustomError = errorBase('CustomError', function (msg) {
this.msg = msg;
});
```## Running tests
Install dev dependencies:
```sh
$ npm i -d && npm test
```## Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/doowb/error-base/issues/new).
## Author
**Brian Woodward**
* [github/doowb](https://github.com/doowb)
* [twitter/doowb](http://twitter.com/doowb)## License
Copyright © 2016 [Brian Woodward](https://github.com/doowb)
Released under the MIT license.***
_This file was generated by [verb](https://github.com/verbose/verb) on February 10, 2016._