Skip to content

Commit

Permalink
Harden stream error handling for the case of a non-Error emit.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjeffburke committed Oct 27, 2023
1 parent d7245be commit 5f2fc80
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/Pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,17 @@ module.exports = class Pipeline extends Stream.Duplex {

// protect against filters emitting errors more than once
stream.once('error', (err) => {
const engineName = this.usedEngines[i].name;
let commandArgs;
if (
(commandArgs = err.commandArgs || this.usedEngines[i].commandArgs)
) {
const engineName = this.usedEngines[i].name;
if (!(err instanceof Error)) {
err = new Error(`${engineName} with index ${i} emitted non-Error`);
commandArgs = null;
} else if (err.commandArgs) {
commandArgs = err.commandArgs;
} else {
commandArgs = this.usedEngines[i].commandArgs;
}
if (commandArgs) {
err.commandLine = `${engineName} ${commandArgs.join(' ')}`;
}
this._fail(err, true);
Expand Down Expand Up @@ -355,7 +361,7 @@ module.exports = class Pipeline extends Stream.Duplex {
addStream(stream) {
this._preflush = true;
this._attach(stream);
this.usedEngines.push({ name: '_stream' });
this.usedEngines.push({ name: '_stream', commandArgs: null });
return this;
}

Expand Down
21 changes: 21 additions & 0 deletions test/impro.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1955,6 +1955,27 @@ describe('impro', () => {
);
});

it('should not break on error from an arbitrary stream that is not an Error', () => {
const erroringStream = new stream.Transform();
erroringStream._transform = () => {
setImmediate(() => {
erroringStream.emit('error', undefined);
});
};

const pipeline = impro.createPipeline().addStream(erroringStream);

return expect(
'bulb.gif',
'when piped through',
pipeline,
'to error with',
expect
.it('to have message', '_stream with index 0 emitted non-Error')
.and('not to have property', 'commandLine')
);
});

it('should not break on error occurring on the internal passthrough', () => {
const error = new Error('arranged error');
class UnchangedStream extends stream.Transform {
Expand Down

0 comments on commit 5f2fc80

Please sign in to comment.