# callbag-catch-error

> Callbag operator that catches errors thrown by pullable and listenable sources

Latest version **1.0.1** (published 2018-09-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install callbag-catch-error
pnpm add callbag-catch-error
yarn add callbag-catch-error
bun add callbag-catch-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 | 1.0.1 |
| Published | 2018-09-03 |
| First published | 2018-08-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 120.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Levent Arman Ozak |
| Maintainers | armanozak |
| Keywords | callbag, callbags |

## Links

- npm: https://www.npmjs.com/package/callbag-catch-error
- Repository: https://github.com/armanozak/callbag-catch-error
- Homepage: https://github.com/armanozak/callbag-catch-error#readme
- Issues: https://github.com/armanozak/callbag-catch-error/issues
- npm.io page: https://npm.io/package/callbag-catch-error

## Recent versions

- 1.0.1 (latest) — 2018-09-03
- 1.0.0 — 2018-08-31

## README

# callbag-catch-error

Callbag operator to catch errors thrown by pullable or listenable sources.

- Handles errors when fetching data, working with promises, etc.
- Proves useful while consuming sources with [callbag-for-each](https://github.com/staltz/callbag-for-each) or [callbag-iterate](https://github.com/staltz/callbag-iterate) by André Staltz.
- Works well with [callbag-throw-error](https://github.com/Andarist/callbag-throw-error) by Mateusz Burzyński.

If you wonder what this is all about, you should definitely check out the awesome [Callbag standard](https://github.com/callbag/callbag) for reactive and iterable programming.

.

## Behavior

- Data from pullable and listenable sources without errors are passed to sinks.
- Errors thrown by pullable and listenable sources get caught.
- Pullable sources stop emitting on error as expected.
- Listenable sources continue emitting after error as expected.

.

## Example 1: Pullable stream

```js
const { flatten, fromIter, fromPromise, map, pipe } = require('callbag-basics');
const iterate = require('callbag-iterate');
const fetch = require('isomorphic-unfetch');
const catchError = require('callbag-catch-error');

pipe(
  fromIter([0, 1]),
  map(n => `https://swapi.co/api/people/${n}`),
  map(url =>
    fetch(url).then(resp => {
      if (!resp.ok) {
        throw new Error('404 Not Found');
      }

      return resp.json();
    }),
  ),
  map(request => fromPromise(request)),
  flatten,
  catchError(err => console.log(err.message)),
  iterate(data => console.log(data.name)),
);

/*

Logs the following:
- 404 Not Found

*/
```

.

## Example 2: Listenable stream

```js
const { flatten, forEach, fromPromise, interval, map, pipe, take } = require('callbag-basics');
const fetch = require('isomorphic-unfetch');
const catchError = require('callbag-catch-error');

pipe(
  interval(10000),
  take(2),
  map(n => `https://swapi.co/api/people/${n}`),
  map(url =>
    fetch(url).then(resp => {
      if (!resp.ok) {
        throw new Error('404 Not Found');
      }

      return resp.json();
    }),
  ),
  map(request => fromPromise(request)),
  flatten,
  catchError(err => console.log(err.message)),
  forEach(data => console.log(data.name)),
);

/*

Logs the following:
- 404 Not Found
- Luke Skywalker

*/
```

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