npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

on-error

v2.1.0

Published

Handle callback errors without the if blocks.

Downloads

17,303

Readme

on-error

NPM version Build Status Coverage Status Davis Dependency Status

Generate error-first callback handlers naturally.

Handle errors via a dedicated error handler function, or by emitting them. Optionally invoke a 2nd function on no error, or regardless of error.

Potentially reduce the number of branches needing tests.

examples

var onError = require('on-error')

// error handler
function handleIt (err) {}

// just do something on error
failToDoSomething(onError(handleIt))
// or emit it
failToDoSomething(onError.emit(emitter))

// if doSomething invokes this generated callback with an error
// handleIt will be called with that error, otherwise our 
// anonymous function will be called with all remaining arguments
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
}))

// or, *gasp*, chain to a promise
doSomething(onError(reject).otherwise(resolve))

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 also be chained from onError.emit()

onError(cb1).otherwiseWithError(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 in-tact.

.otherwiseWithError may also 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 also 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 also be chained from onError.emit()

testing

npm test [--dot | --spec] [--grep=pattern]

Specifying --dot or --spec will change the output from the default TAP style. Specifying --grep will only run the test files that match the given pattern.

coverage

npm run coverage [--html]

This will output a textual coverage report. Including --html will also open an HTML coverage report in the default browser.