4

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);
});

logs

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

I expected it to log

fark
fark
fark

How do I use errors correctly in highland.js?

wprl
  • 24,489
  • 11
  • 55
  • 70
Jehan
  • 2,701
  • 2
  • 23
  • 28

2 Answers2

2

Reading the source of the map function linked on http://highlandjs.org/#map you can see that the function is applied like this :

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

so if you want to transform a value into a highland StreamError via the map transformer, you need to throw the 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
Jerome WAGNER
  • 21,986
  • 8
  • 62
  • 77
0

In the documentation you have this :

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 still at the following line :

push(null, 'fark');

try with

push(err);

Hope this help you !

Francois Borgies
  • 2,378
  • 31
  • 38
  • I was actually trying to recreate the `if` statement, where it gets an error and pushes a different value instead of propagating the error itself. I'm still pretty shaky on how errors work in general in this library. – Jehan Feb 28 '14 at 19:38