slenderr

0.1.1 • Public • Published

slenderr

defines the error object of your own. inherits the Error.prototype. but does not let you perform multiple inheritance.

synopsis

var Slenderr = require('slenderr')
// defines "CookieParseError"
Slenderr.define('cookie parse')
// can use "Slenderr.CookieParseError"
var err = new Slenderr.CookieParseError('not found response.headers.cookie')
console.log(err.name)    // 'CookieParseError'
console.log(err.message) // 'not found response.headers.cookie'
console.log(err.json())  // '{"name":"CookieParseError","message":"not found response.headers.cookie"}'

in browser

<script src="path/to/slenderr/lib/slenderr.js"></script>
Slenderr.define(...)

or with browerify.

methods

define(name[, defaultMessage [, defaultOption]])

defines the Constructor and attaches to Slenderr.

  • name {String} required error name. this name is camelized automatically.
  • defaultMessage {String} optional default error message.
  • defaultOption {Object} optional the pair of the properties to add to error.

defined constructors

defined the Constructor is camelized and attaches to Slenderr.

Slenderr.define(error_name)
var err = new Slenderr.ErrorNameError(message[, option])
  • message {String} optional error message. override default error message.
  • option {Object} optional additional properties. override default option.
Slenderr.define('cookie parse', 'can not parse cookie', {code: 400})
// case 1
var err = new Slenderr.CookieParseError()
console.log(err.name)    // 'CookieParseError'
console.log(err.message) // 'can not parse cookie'
console.log(err.code)    // 400
// case 2
var err = new Slenderr.CookieParseError(
    'not found cookie in response.headers'
  , {code: 499}
)
console.log(err.message) // 'not found cookie in response.headers'
console.log(err.code)    // 499

inherits

the constructed error object is inherits from Error.prototype.

Slenderr.define('foo').define('bar')
 
var err = new Slenderr.BarError('foo is not bar')
console.log(err instanceof Error) // true
console.log(err instanceof Slenderr.BarError) // true
console.log(err instanceof Slenderr.FooError) // false

stacktrace

...

to JSON string

exports to the JSON string containing your own error object.

Slenderr.define('foo', 'bar is not foo', {code: 100})
 
console.log((new Slenderr.FooError).json())
// {"code":100,"name":"FooError","message":"bar is not foo"}

test

$ npm test

with browserify and testling

$ npm run testling

tips: defines in the small scope

use call or apply

function scp () {
    var errs = {}
    Slenderr.define.call(errs, 'err_name', ...)
    var err = new errs.ErrNameError(...)
    ...
}

license

MIT

Readme

Keywords

Package Sidebar

Install

npm i slenderr

Weekly Downloads

0

Version

0.1.1

License

MIT

Last publish

Collaborators

  • ishiduca