gm icon indicating copy to clipboard operation
gm copied to clipboard

Enable error handling when streaming

Open paulmelnikow opened this issue 7 years ago • 4 comments

See discussion of the problem in #256 (and a bit more in #548).

Adding proc to the .stream() callback seems like the easiest way to allow the caller to handle this. Tested in a downstream package and it works, though it's not terribly convenient.

When no callback is provided, emit an error event on the throughStream.

Before and after this change, I have two failing tests, which appear to be unrelated.

paulmelnikow avatar Mar 28 '17 19:03 paulmelnikow

Same errors are appearing in CI. Again, I don't think they are related to this change.

paulmelnikow avatar Mar 28 '17 19:03 paulmelnikow

@aheckmann Could you take a look, please? Would be great to get this fixed for badges/shields#914.

paulmelnikow avatar Mar 31 '17 15:03 paulmelnikow

Hi @aheckmann , this is an important PR, otherwise the use of .stream() is unreliable. I guess the PR would have to get rebased, if that's done, would you merge it? Thanks.

pmoleri avatar Jan 21 '19 14:01 pmoleri

Hello, I bumped into this in one of my projects today and I more or less solved it by bypassing .stream() like the following. And if somebody else stumbles onto this here is one solution to at least notice the warnings

async function gmToBuffer (data) {
  return new Promise((resolve, reject) => {
    let chunks = [];
    data._preprocess((err) => {
      if (err) return reject(err);

      data._spawn(data.args(), false, (err, stdout, stderr) => {
        if (err) return reject(err);

        stdout.on('data', (chunk) => chunks.push(chunk));
        stdout.once('end', () => resolve(Buffer.concat(chunks)));
        stderr.on('data', (msg) => {
          const error_message = String(msg);
          if (error_message.includes('@ warning/')) {
            logger.error(error_message);
          } else {
            stdout.emit('error', new Error(error_message));
          }
        });
        stdout.once('error', (err) => reject(err));
      });
    });
  });
}

And to be specific I wanted to still get the warning and not just silence them.

jonlil avatar Jun 29 '23 14:06 jonlil