By default, if an error happens when applying the transform of a line on a particular
item from its content, this error will break the line and the rest of the line's content will also
remain unprocessed. To handle such errors more gracefully, you can use the handleError()
function:
1import { line, handleError } from 'rxline';
2
3
4function triple(x) {
5 if (x == 2) throw Error('I HATE 2!!');
6 else return x * 3;
7}
8
9line([1, 2, 3, 4])
10.pipe(
11 triple, // --> triple the number
12 handleError(error => console.log(error.message)) // --> log the errors
13)
14.collect(console.log);
15
16// Result:
17// > I HATE 2!!
18// > [3, 9, 12]
The error handler function will also be provided with the input in the line content which caused the issue:
1line([1, 2, 3, 4])
2.pipe(
3 triple,
4 handleError((err, num) => { // --> argument `num` is the number on which the error occured.
5 console.log(`ERROR FOR ${num}:: ${err.message}`);
6 })
7)
8.collect(console.log);
9
10// Result:
11// > ERROR FOR 2:: I HATE 2!!
12// > [3, 9, 12]
The error handler function is also provided with a rethrow
callback, which it can use to rethrow the error:
1line([1, 2, 3, 4])
2.pipe(
3 triple,
4 handleError((err, num, rethrow) => {
5 console.log(`ERROR FOR ${num}`);
6 rethrow(err);
7 })
8)
9.collect(console.log);
10
11// Result:
12// > ERROR FOR 2
13// > I HATE 2!!
error IMPORTANT
Note that the
handleError()
function will only be able to handle errors occuring before it in the line and not those that happen after.