HandleError

By default, if an error is thrown somewhere in a reactive flow, the whole flow or parts of it will shut-down afterwards. handleError() allows you to catch errors and handle them gracefully:
  /*!*/import { source, map, handleError, sink } from '@connectv/core';

let a = source();

//
//--> this flow does not have error handling, so it will die
//--> out after an error occurs..
//
a.to(map(x => {
/*!*/  if (x == 2) throw new Error();
  else return x;
})).subscribe(v => console.log('A:: ' + v));

//
//--> this flow has error handling, so it will continue after
//--> an error occurs.
//
a.to(map(x => {
/*!*/  if (x == 2) throw new Error();
  else return x;
}))
/*!*/.to(handleError())
.serialTo(sink(v => console.log('B:: ' + v)))
.subscribe();

a.send(1);  //--> logged by both
a.send(2);  //--> ignored by both
a.send(3);  //--> logged by 'B:: ' only, since 'A:: ' flow is dead.

Catching errors

Accessing the thrown error object with implicit connection looks like this:
  /*!*/import { source, map, group, handleError, pin } from '@connectv/core';

let a = source();

a.to(map(x => {
  if (x == 2) throw new Error();
  else return x;
}))
/*!*/.to(handleError())
/*!*/.serialTo(
/*!*/  pin(),                                     //--> the usual output
/*!*/  map(e => `error for  ${e.emission.value}`) //--> the error output
).subscribe(console.log);

a.send(1);
a.send(2);
a.send(3);
You could have also accessed it explicitly via its .out("error") output (or using .error shortcut property):
/*!*/let h = handleError();

...

/*!*/h.error
  .to(map(e => `error for ${e.emission.value}`))
  .subscribe(console.log);


Signature

Each handleError() has an "input" input (short-hand property: .input), on which it receives incoming emissions. When no error has occured, it will simply relay the emission on its "output" output (short-hand property: .output). In case of error, the error object will be emitted via its "error" output (short-hand property: .error):
let h = handleError();

h.in("input") == h.input    //--> receives emissions on this
h.out("output") == h.output //--> emits them on this where there is no error
h.out("error") == h.error   //--> emits errors on this pin.


Further reading




Copied to Clipboard!