callbag-catch-error
v1.0.1
Published
Callbag operator that catches errors thrown by pullable and listenable sources
Downloads
2
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 or callbag-iterate by André Staltz.
- Works well with callbag-throw-error by Mateusz Burzyński.
If you wonder what this is all about, you should definitely check out the awesome Callbag standard 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
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
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
*/