0.0.2 • Published 10 years ago

stream-try-catch v0.0.2

Weekly downloads
34
License
-
Repository
github
Last release
10 years ago

stream-try-catch

a try-catch like library for stream pipelines

build status

Example

try catch errors on a pipeline

var _try = require('stream-try-catch').try;

// pipe streams
var stream = _try(streamA).pipe(streamB).pipe(streamC).catch(onerror);

// regular piping takes place
assert(stream == streamC);

// propagates errors
var onerror = function(err) {
  // called three times
});

streamA.emit('error', new Error);
streamB.emit('error', new Error);
streamC.emit('error', new Error);

API

try(src)

Begin the setup of try/catch sequence on a pipeline

Any call to try(src).pipe(dest, opt) will automatically

  • forward all 'error' events from src to dest.
  • make sure dest is instrumented in the same way (just as if try(dest) had been called)

you can catch errors on the pipeline either by adding a classic on('error', onerror) on the last stream of the pipeline or use the syntactic sugar catch

try(streamA).pipe(streamB).pipe(streamC).catch(onerror);

This will forward all errors from streamA, streamB and streamC to onerror

Note that a catch stops the try/catch block so in

var stream = try(streamA).pipe(streamB).catch(onerror).pipe(streamC);

the errors from A and B will bubble to onerror but they will not bubble to streamC.

In a way, the try/catch guards

Installation

$ npm install stream-try-catch

Licence

MIT