Welcome to the Linux Foundation Forum!

Writable constructor error handling

Hi all,

I am trying out the behavior of the Writable constructor for error event handling.
And using the below code block I was not able to get the on('error', ... event handler to fire.
May I know if I am coding this wrong? My node is version v14.15.3. Thanks for any help in advance.

const createWriteStream = (data) => {
  return new Writable({
    decodeStrings: false,
    write(chunk, enc, next) {
      data.push(chunk);
      next();
    },
  });
};

const data = [];
const writable = createWriteStream(data);
writable.on('finish', () => {
  console.log('finished writing', data);
});
writable.on('error', (e) => {
  console.error('error event:', e);
});
writable.write(1);
writable.end('nothing more to write');

Comments

  • On Node 14 this particular case causes an error to throw synchronously, it doesn't go through the error handler. Curiously on Node 12 it does go through the error handler. I think you've found an odd case that might be worth reporting on Node core.

    If you want to trigger the error handler just to see it working, pass an error object to the next function.

Sign In or Register to comment.