blob: f4f8eb14c7f756d712097f063b741750455ae707 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
makeerror [![Build Status](https://secure.travis-ci.org/nshah/nodejs-makeerror.png)](http://travis-ci.org/nshah/nodejs-makeerror)
=========
A library to make errors.
Basics
------
Makes an Error constructor function with the signature below. All arguments are
optional, and if the first argument is not a `String`, it will be assumed to be
`data`:
```javascript
function(message, data)
```
You'll typically do something like:
```javascript
var makeError = require('makeerror')
var UnknownFileTypeError = makeError(
'UnknownFileTypeError',
'The specified type is not known.'
)
var er = UnknownFileTypeError()
```
`er` will have a prototype chain that ensures:
```javascript
er instanceof UnknownFileTypeError
er instanceof Error
```
Templatized Error Messages
--------------------------
There is support for simple string substitutions like:
```javascript
var makeError = require('makeerror')
var UnknownFileTypeError = makeError(
'UnknownFileTypeError',
'The specified type "{type}" is not known.'
)
var er = UnknownFileTypeError({ type: 'bmp' })
```
Now `er.message` or `er.toString()` will return `'The specified type "bmp" is
not known.'`.
Prototype Hierarchies
---------------------
You can create simple hierarchies as well using the `prototype` chain:
```javascript
var makeError = require('makeerror')
var ParentError = makeError('ParentError')
var ChildError = makeError(
'ChildError',
'The child error.',
{ proto: ParentError() }
)
var er = ChildError()
```
`er` will have a prototype chain that ensures:
```javascript
er instanceof ChildError
er instanceof ParentError
er instanceof Error
```
|