1 | create-error.js
|
2 | ===============
|
3 |
|
4 | A 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
|
16 | var createError = require('create-error');
|
17 |
|
18 | var MyCustomError = createError('MyCustomError');
|
19 | var SubCustomError = createError(MyCustomError, 'CoolSubError', {messages: []});
|
20 |
|
21 | var sub = new SubCustomError('My Message', {someVal: 'value'});
|
22 |
|
23 | sub instanceof SubCustomError // true
|
24 | sub instanceof MyCustomError // true
|
25 | sub instanceof Error // true
|
26 |
|
27 | assert.deepEqual(sub.messages, []) // true
|
28 | assert.equal(sub.someVal, 'value') // true
|
29 | ```
|
30 |
|
31 | ### createError(name, [properties])
|
32 |
|
33 | Creates a new error by specifying the name of the error to be created,
|
34 | taking an optional hash of properties to be attached to the error class
|
35 | upon creation.
|
36 |
|
37 | ### createError(Target, [name, [properties]])
|
38 |
|
39 | Create a new error by specifying the `Target` error class we wish to inherit from,
|
40 | along with an optional name and properties for the error. If the `name` is omitted,
|
41 | it will have the same name as the parent error.
|
42 |
|
43 | ### Additional Notes:
|
44 |
|
45 | In the browser, the function will be assigned to `window.createError`,
|
46 | and `createError.noConflict()` will restore the original `window.createError`
|
47 | if overwritten.
|
48 |
|
49 | ### License
|
50 |
|
51 | MIT
|