Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

assert-err

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

assert-err

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

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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

Keywords

FAQs

Package last updated on 13 Sep 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc