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
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')
return a + b
}
add(1, 'no')
Example 2: Error props
If using global.Error, assertErr accepts a props
argument to extend error
assertErr(false, Error, 'message', { code: 1, status: 'status' })
Example 3: args
assertErr supports up to 5 err constructor args
assertErr(false, CustomError)
assertErr(false, CustomError, 'foo')
assertErr(false, CustomError, 'foo', 'bar')
assertErr(false, CustomError, 'foo', 'bar', 'qux')
assertErr(false, CustomError, 'foo', 'bar', 'qux', 'corge')
assertErr(false, CustomError, 'foo', 'bar', 'qux', 'corge', 'yolo')
assertErr(false, CustomError, 'foo', 'bar', 'qux', 'corge', 'yolo', 'toomany')
License
MIT