UNPKG

1.41 kBMarkdownView Raw
1create-error.js
2===============
3
4A simple helper for creating subclassed errors in Javascript.
5
6[![Build Status](https://travis-ci.org/tgriesser/create-error.png)](https://travis-ci.org/tgriesser/create-error)
7
8## Use:
9
10```bash
11$ npm install create-error
12$ bower install create-error
13```
14
15```js
16var createError = require('create-error');
17
18var MyCustomError = createError('MyCustomError');
19var SubCustomError = createError(MyCustomError, 'CoolSubError', {messages: []});
20
21var sub = new SubCustomError('My Message', {someVal: 'value'});
22
23sub instanceof SubCustomError // true
24sub instanceof MyCustomError // true
25sub instanceof Error // true
26
27assert.deepEqual(sub.messages, []) // true
28assert.equal(sub.someVal, 'value') // true
29```
30
31### createError(name, [properties])
32
33Creates a new error by specifying the name of the error to be created,
34taking an optional hash of properties to be attached to the error class
35upon creation.
36
37### createError(Target, [name, [properties]])
38
39Create a new error by specifying the `Target` error class we wish to inherit from,
40along with an optional name and properties for the error. If the `name` is omitted,
41it will have the same name as the parent error.
42
43### Additional Notes:
44
45In the browser, the function will be assigned to `window.createError`,
46and `createError.noConflict()` will restore the original `window.createError`
47if overwritten.
48
49### License
50
51MIT