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