How to use highland.js map errors

This function

var _ = require('highland');
var accounts = ['ack', 'bar', 'foo'];

_(accounts).map(function (x) {
  return new Error(x);
}).errors(function (err, push) {
  push(null, 'fark');
}).each(function (x) {
  console.log(x);
});

magazines

[Error: ack]
[Error: bar]
[Error: foo]

I expected him to record

fark
fark
fark

How to use errors in highland.js correctly?

+3
source share
2 answers

Reading the source of the function mapassociated with http://highlandjs.org/#map , you can see that the function is applied as follows:

var fnVal, fnErr;
try {
   fnVal = f(x);
} catch (e) {
    fnErr = e;
}
push(fnErr, fnVal);

so if you want to convert a value to a streaming StreamError using a transformer map, you need to throw an error.

The following code:

var _ = require('highland');
var accounts = ['ack', 'bar', 'foo'];

_(accounts).map(function (x) {
  throw new Error(x);
}).errors(function (err, push) {
  push(null, 'fark');
}).each(function (x) {
  console.log(x);
});

will give you the expected

fark
fark
fark
+1
source

In the documentation you have the following:

getDocument.errors(function (err, push) {
    if (err.statusCode === 404) {
        // not found, return empty doc
        push(null, {});
    }
    else {
        // otherwise, re-throw the error
        push(err);
    }
});

So, I think your error is still on the following line:

push(null, 'fark');

try

push(err);

Hope this helps you!

0
source

All Articles