Easily subclass and customize new Error types
To include in your project:javascript var errorEx = require('error-ex');
To create an error message type with a specific name (note, that ErrorFn.name
will not reflect this):
```javascript
var JSONError = errorEx('JSONError');
var err = new JSONError('error');
err.name; //-> JSONError
throw err; //-> JSONError: error
```
To add a stack line:
```javascript
var JSONError = errorEx('JSONError', {fileName: errorEx.line('in %s')});
var err = new JSONError('error')
err.fileName = '/a/b/c/foo.json';
throw err; //-> (line 2)-> in /a/b/c/foo.json
```
To append to the error message:
```javascript
var JSONError = errorEx('JSONError', {fileName: errorEx.append('in %s')});
var err = new JSONError('error');
err.fileName = '/a/b/c/foo.json';
throw err; //-> JSONError: error in /a/b/c/foo.json
```
errorEx([name], [properties])
Creates a new ErrorEx error type
name
: the name of the new type (appears in the error message upon throw;Error.name
)properties
: if supplied, used as a key/value dictionary of properties toline
: if specified and is a function, return value is added as a stackstack
: if specified and is a function, passed the value of the propertymessage
: if specified and is a function, return value is used as new.message
value upon get. Passed the property value of the property namedReturns a constructor (Function) that can be used just like the regular Error
constructor.
var errorEx = require('error-ex');
var BasicError = errorEx();
var NamedError = errorEx('NamedError');
// --
var AdvancedError = errorEx('AdvancedError', {
foo: {
line: function (value, stack) {
if (value) {
return 'bar ' + value;
}
return null;
}
}
}
var err = new AdvancedError('hello, world');
err.foo = 'baz';
throw err;
/*
AdvancedError: hello, world
bar baz
at tryReadme() (readme.js:20:1)
*/
errorEx.line(str)
Creates a stack line using a delimiter
This is a helper function. It is to be used in lieu of writing a value object
forproperties
values.
str
: The string to create%s
to specify where in the string the value should govar errorEx = require('error-ex');
var FileError = errorEx('FileError', {fileName: errorEx.line('in %s')});
var err = new FileError('problem reading file');
err.fileName = '/a/b/c/d/foo.js';
throw err;
/*
FileError: problem reading file
in /a/b/c/d/foo.js
at tryReadme() (readme.js:7:1)
*/
errorEx.append(str)
Appends to the error.message
string
This is a helper function. It is to be used in lieu of writing a value object
forproperties
values.
str
: The string to append%s
to specify where in the string the value should govar errorEx = require('error-ex');
var SyntaxError = errorEx('SyntaxError', {fileName: errorEx.append('in %s')});
var err = new SyntaxError('improper indentation');
err.fileName = '/a/b/c/d/foo.js';
throw err;
/*
SyntaxError: improper indentation in /a/b/c/d/foo.js
at tryReadme() (readme.js:7:1)
*/
Licensed under the MIT License.
You can find a copy of it in LICENSE.