Custom error constructor factory.
Custom error constructor factory.
tea-error is available on npm.
$ npm install tea-error
The primary export for this module is a factory that will return a constructor for a custom error of the type/name that is specified.
var error = require'tea-error'var TestError = error'TestError'var err = 'My error message.'throw err
An error created/thrown from the custom constructor
The constructed error is an instanceof an Error.
var err =var myerr =errshouldbeinstanceofErrormyerrshouldbeinstanceofErrormyerrshouldbeisntanceofTestError
name property is defined like other "native" errors.
var err =var myerr =errshouldhaveproperty'name' 'ReferenceError'myerrshouldhaveproperty'name' 'TestError'
message property is defined like all error and specified
as the first argument during construction.
If the environment provides the ability to capture a stack trace,
it will be provided as the
stack property. Please view your environments
documentation to see if this is supported.
var err =var myerr =if ErrorcaptureStackTraceerrshouldhaveproperty'stack'myerrshouldhaveproperty'stack'
var TestError = require'tea-errors''TestError'var err = 'that did not work right'throw err
There are a number of additional arguments that can be specified upon construction to provide further insight into the error created.
The second argument during construction can be an object of properties that will be merged onto the newly created error.
var err = 'expected field value' fields: 'username'errshouldhaveproperty'fields'an'array'thatdeepequals 'username'
Note that if the
stack properties are defined
in this custom properties object, they will be ignored as those keys
The start stack function is used by
to indicate where the environment should start the visible
stack trace. Modifying this value might be useful in situations
where the internals of the module issuing the error are irrelevant
to what occured. The most obvious example is providing feedback
for an api method.
// api.jsvar ApiError = require'tea-error''ApiError'if 'function' !== typeof fnthrow 'API .use only accepts functions' null argumentscallee;// etc.// user.jsvar api = require'./api.js)api.use('a string?')
In this scenario the first line of the error stack will reference
user.js-Ln:2 as opposed to
api.js-Ln:5. See the
for a demonstration.
Convert this error into a serialized JSON object.
var err = 'some message' hello: 'universe'var json = errtoJSON;jsonshoulddeepequalname: 'TestError'message: 'some message'hello: 'universe'stack: '...';
If this method is called as
stack property will not be included.
(The MIT License)
Copyright (c) 2012 Jake Luer firstname.lastname@example.org (http://qualiancy.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.