# on-error

> Handle callback errors without the if blocks.

Latest version **2.1.0** (published 2015-07-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install on-error
pnpm add on-error
yarn add on-error
bun add on-error
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2015-07-22 |
| First published | 2014-12-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Jason Pincin |
| Maintainers | jasonpincin |
| Keywords | error, callback, emit, event, emitter |

## Links

- npm: https://www.npmjs.com/package/on-error
- Repository: https://github.com/jasonpincin/on-error
- Issues: https://github.com/jasonpincin/on-error/issues
- npm.io page: https://npm.io/package/on-error

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 2.1.0 (latest) — 2015-07-22
- 2.0.2 — 2015-01-24
- 2.0.1 — 2015-01-23
- 2.0.0 — 2015-01-23
- 1.1.0 — 2014-12-08
- 1.0.0 — 2014-12-08

## README

# on-error

[![NPM version](https://badge.fury.io/js/on-error.png)](http://badge.fury.io/js/on-error)
[![Build Status](https://travis-ci.org/jasonpincin/on-error.svg?branch=master)](https://travis-ci.org/jasonpincin/on-error)
[![Coverage Status](https://coveralls.io/repos/jasonpincin/on-error/badge.png?branch=master)](https://coveralls.io/r/jasonpincin/on-error?branch=master)
[![Davis Dependency Status](https://david-dm.org/jasonpincin/on-error.png)](https://david-dm.org/jasonpincin/on-error)

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

```javascript
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

```javascript
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.

---
_Source: https://npm.io/package/on-error · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
