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

on-error

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

on-error

Execute error handler instead of, or in addition to callback, when 1st callback argument is truthy

  • 1.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.2K
decreased by-56.34%
Maintainers
1
Weekly downloads
 
Created
Source

on-error

Build Status Coverage Status NPM version Davis Dependency Status

Handle callback errors by executing an error handler function, or emitting the error, instead of or in addition to executing the callback.

example

var onError = require('on-error')

function fail (cb) {
    cb(new Error('failed'))
}

function succeed (cb) {
    cb(null, 'success')
}

Using error handler function:

function handleIt (err) {
    console.error(err)
}

fail(onError(handleIt, function (status) {
    console.log('will not see this')
}))

fail(onError(handleIt, {alwaysCall: true}, function (err, status) {
    // When always call specified, err is passed
    console.log('will see this')
}))

succeed(onError(handleIt, function (status) {
    // Gets called with status of success
    console.log(status)
}))

Using emitter:

var EventEmitter = require('events').EventEmitter
var emitter = new EventEmitter().on('error', function (err) {
    console.error(err)
})

fail(onError.emit(emitter, function (status) {
    console.log('will not see this')
}))

api

onError

var onError = require('on-error')

var wrappedCb = onError(errHandler [, options, cb])

Returns a function that when called with a truthy first argument, will execute the errHandler with the 1st (error) argument supplied to the wrappedCb. If the option alwaysCall is define, the provided cb will be executed in all cases with all arguments supplied to wrappedCb, otherwise if the 1st argument to wrappedCb is falsey, the supplied cb will be executed with all but the 1st argument supplied to wrappedCb.

If no callback cb is provided, then the generated callback will simply execute errHandler when called with a non-falsey 1st argument.

options:

  • alwaysCall: true or false - if true, the provided callback cb will always be called (and include the 1st argument), otherwise it will only be called when a the first argument is falsey (and without the 1st argument)

var wrappedCb = onError.emit(emitter [, options, cb])

Same behaviour as above, except errors will be emitted to emitter instead of passed to an error handler function. All other options and arguments are the same.

testing

npm test [--dot | --spec] [--coverage]

options

  • --dot - output test results as dots instead of tap
  • --spec - output test results as spec instead of tap
  • --coverage - display text cover report
  • --testling - run tests in browser via testling (cannot be used with --coverage and expects both browserify and testling to be installed globally)

patterns

Only run test files matching a certain pattern by prefixing the test command with grep=pattern. Example:

grep=connect npm test --dot

html coverage report

Open it with npm run view-cover or npm run vc

Keywords

FAQs

Package last updated on 08 Dec 2014

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