0.3.1 • Published 13 years ago
typederror v0.3.1
typederror
custom error type creator: throw new CustomError()
installation
$ npm install typederrorusage
var typedError = require('typederror')
var MyError = typedError('MyError', {code: 0x1d107, message: 'Not a winning gamepiece.'})
var e = new MyError()
e.code
// => 0x1d107
e.message
// => 'Not a winning gamepiece.'
e.toString()
// => 'MyError: Not a winning gamepiece.'It'll give you a stack trace, just like a real Error!
e.stack
// => your normal stack trace-y stuffIt's instanceof Error
e instanceof Error
// => trueYou can pass inner error messages!
if (err) {
throw new MyError('We gots an error!', err);
}
// err is available on the newly thrown error's `.inner` propertyapi
typedError(name, opts) => TypedError
name should be your the name of your error. By convention, it should be PascalCase and end in Error.
opts is an optional object with any of these properties:
message: default message for this error typecode: value representing this type of error (for example, to map to an existing code set, like HTTP status codes)
Returns a TypedError constructor function.
TypedError() or TypedError(message) or TypedError(inner) or TypedError(message, inner)
Constructor. May be called with or without new keyword.
message is a String. inner is an Error, an error to attach to the inner property. Use when throwing a new error in response to another error.
contributors
jden jason@denizac.org
license
MIT. (c) 2013 Agile Diagnosis hello@agilediagnosis.com. See LICENSE.md