assert-err
assertion library that throws user-specified error types (accepts error class and message)
Last updated 8 years ago by tjmehta .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install assert-err 
SYNC missed versions from official npm registry.

assert-err

assertion library that throws user-specified error types (accepts error class and message)

Installation

npm i --save assert-err

Usage

Example 1: full example

// example-1.js
var assertErr = require('assert-err')

function add (a, b) {
  assertErr(typeof a === 'number', TypeError, '"a" must be a number')
  assertErr(typeof b === 'number', TypeError, '"b" must be a number') //  line 6
  return a + b
}

add(1, 'no') // line 10
/*
TypeError: "b" must be a number
    at add (example-1.js:6:3)
    at Object.<anonymous> (example-1.js:10:1)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:139:18)
    at node.js:999:3
*/

Example 2: Error props

If using global.Error, assertErr accepts a props argument to extend error

assertErr(false, Error, 'message', { code: 1, status: 'status' }) // 0 args
// { [Error: boom] code: 1, status: 'status' }

Example 3: args

assertErr supports up to 5 err constructor args

assertErr(false, CustomError) // 0 args
assertErr(false, CustomError, 'foo') // 1 args
assertErr(false, CustomError, 'foo', 'bar') // 2 args
assertErr(false, CustomError, 'foo', 'bar', 'qux') // 3 args
assertErr(false, CustomError, 'foo', 'bar', 'qux', 'corge') // 4 args
assertErr(false, CustomError, 'foo', 'bar', 'qux', 'corge', 'yolo') // 5 args
// error...
assertErr(false, CustomError, 'foo', 'bar', 'qux', 'corge', 'yolo', 'toomany') // 6 args
// Error: assertErr does not support more than five Error args

License

MIT

Current Tags

  • 1.1.0                                ...           latest (8 years ago)

2 Versions

  • 1.1.0                                ...           8 years ago
  • 1.0.0                                ...           8 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (4)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE