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

Handle callback errors without the if blocks.

  • 2.0.1
  • Source
  • npm
  • Socket score

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

on-error

NPM version Build Status Coverage Status Davis Dependency Status

Handle callback errors without the if blocks.

On error, execute separate error handler function, or emit it. On no error, execute regular callback
(with or without error stripped).

example

// if error handleIt, otherwise...
doSomething(onError(handleIt).otherwise(function (message) {
    console.log('will see this: %s', message)
}))

failToDoSomething(onError(handleIt).otherwise(function (message) {
    console.log('will NOT see this: %s', message)
}))

// if error handleIt, and always...
failToDoSomething(onError(handleIt).always(function (message) {
    console.log('will see this too: %s', message)
}))

// if error handleIt, and always (include error for always func)...
failToDoSomething(onError(handleIt).alwaysWithError(function (err, message) {
    console.log('will see this too (with the error): %s, %s', err, message)
}))

// maybe we want to emit the error instead...
var emitter = new EventEmitter().on('error', console.log)
failToDoSomething(onError.emit(emitter).otherwise(function (message) {
    // will not get here
}))

api

var onError = require('on-error')

onError(cb)

Returns a function that when called with a truthy first argument, will execute cb with said argument.

onError.emit(emitter)

Returns a function that when called with a truthy first argument, will emit error on the provided event emitter with said argument.

onError(cb1).otherwise(cb2)

Returns a function that when called with a truthy first argument, will execute cb1 with said argument. When executed with a non-truthy 1st argument, cb2 will instead be executed with the error argument stripped.

.otherwise may always be chained from onError.emit()

onError(cb1).always(cb2)

Returns a function that when called with a truthy first argument, will execute cb1 with said argument. In addition, cb2 will always be executed with the error argument stripped.

.always may always be chained from onError.emit()

onError(cb1).alwaysWithError(cb2)

Returns a function that when called with a truthy first argument, will execute cb1 with said argument. In addition, cb2 will always be executed with the error argument in-tact.

.alwaysWithError may always be chained from onError.emit()

testing

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

Alternatively, only run test files matching a certain pattern by prefixing the command with grep=pattern. Example: grep=init npm test

Open an html coverage report after running tests with npm run view-cover or npm run vc

Keywords

FAQs

Package last updated on 23 Jan 2015

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