Détail du package

kind-error

tunnckocore22MIT2.0.0

Base class for easily creating meaningful and quiet by default Error classes with sane defaults and assertion in mind.

assert, assertion, base, check

readme

kind-error npmjs.com The MIT License

Base class for easily creating meaningful and quiet by default Error classes.

code climate standard code style travis build status coverage status dependency status

Install

npm i kind-error --save

Features

  • You can customize error name with name property in options object.
  • By default won't have stack property in the composed error object.
  • You should pass showStack: true in options if you want stacktraces.
  • Composes meaningful error output if actual and expected given.
  • If actual and expected is given, will compose message automatically.

Usage

For more use-cases see the tests

const KindError = require('kind-error')

KindError

Initialize KindError class with message and options.

Params

Example

const err = new KindError('msg', {
  showStack: true,
  custom: 123
})

console.log(err) // => [KindError: msg]
console.log(err.custom) // => 123
console.log(err.stack) // => error stack trace

Or if you give actual and expected it will make default message. See this example.

const assert = require('assert')
const err = new KindError({
  actual: [1, 2, 3],
  expected: {foo: 'bar'}
})

assert.deepEqual(err.actual, [1, 2, 3])
assert.deepEqual(err.expected, {foo: 'bar'})
assert.strictEqual(err.stack, undefined)
assert.strictEqual(err.type.actual, 'array')
assert.strictEqual(err.type.expected, 'object')
assert.strictEqual(err.inspect.actual, '[ 1, 2, 3 ]')
assert.strictEqual(err.inspect.expected, '{ foo: \'bar\' }')
assert.strictEqual(err.message, 'expect `object`, but `array` given')

Example AppError

Here we show how to create new error class

var util = require('util')
var KindError = require('kind-error')

function AppError () {
  KindError.apply(this, arguments)
  this.name = 'AppError'
}

util.inherits(AppError, KindError)

AppError.prototype.foo = function () {
  return 123
}

var err = new AppError('foo bar', {baz: 'qux'})
//=> err
// err.name => 'AppError'
// err.message => 'foo bar'
// err.baz => 'qux'
// err.foo() => 123

Related

  • abbrev-kindof: Use abbreviations for checking type of given value. Like kindof(val, 'soa') to check that value is string, object or array.
  • assert-kindof: Check native type of the given value and throw TypeError if not okey. Expressive, elegant, behavior-driven API, good descriptive default error messages, simple and clean syntax.
  • is-kindof: Check type of given javascript value. Support promises, generators, streams, and native types. Thin wrapper around kind-of module.
  • kind-of-extra: Extends kind-of type check utility with support for promises, generators, streams and errors. Like kindof(Promise.resolve(1)) //=> 'promise' and etc.
  • kind-of-types: List of all javascript types. Used and useful for checking, validation, sanitizing and testing. Like isStream, isPromise, isWeakset and etc.
  • error-base: Create custom Error classes.

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.

Charlike Make Reagent new message to charlike freenode #charlike

tunnckocore.tk keybase tunnckocore tunnckoCore npm tunnckoCore twitter tunnckoCore github

changelog

2.0.0 - 2016-02-11

  • Release v2.0.0 / npm@v2.0.0
  • add more defaults - err.type, err.inspect and err.orig containing:
    • all of them exists only if actual and expected are given
    • all of them have .actual and .expected properties
    • err.type contains the type of actual and expected
    • err.inspect contains the inspected value using util.inspect useful for outputs
    • err.orig contains the original values (same as err.actual and err.expected)
  • change default output of .toString()
  • allow message option to be function when actual and expected given
  • signals/forces you to install kind-of-extra when actual and expected are given
  • add detailed option for more detailed message (composed only when actual and expected are given)
  • update repo boilerplate
  • update license year range
  • use error-base
  • major refactor #74188f (more info above)

1.3.0 - 2015-09-03

  • Release v1.3.0 / npm@v1.3.0
  • update tests and features list
  • implement .toString method
  • update, bump deps

1.2.0 - 2015-07-01

  • Release v1.2.0 / npm@v1.2.0
  • allow opts.actual to be falsey value, fix and close #3

1.1.0 - 2015-07-01

  • Release v1.1.0 / npm@v1.1.0
  • codeclimate: fix complexity
  • codeclimate: disable jshint newcap
  • add creating AppError example
  • add creating custom error class test
  • allow invoking without new keyword
  • update usage
  • refactor, add more tests

1.0.1 - 2015-07-01

  • Release v1.0.1 / npm@v1.0.1
  • update related section
  • update tests
  • switch deps

1.0.0 - 2015-06-30

  • Release v1.0.0 / npm@v1.0.0
  • add related section
  • reorder it a bit
  • update readme exampoles/features
  • add this.value prop, when this.actual
  • add test, composed error is instanceof Error
  • cleanup
  • update readme
  • add keywords
  • complexity and codeclimate
  • add test for auto-creating message
  • add little assertion sugar
  • stack logic
  • add test for .toString
  • implement :star2:

0.0.0 - 2015-06-29

  • Initial commit