diff --git a/package.json b/package.json index eaa0bff..c30eb11 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ ], "author": "Quentin Rossetti ", "contributors": [ - {"name": "Jessé LIBEN", "email": "jliben@gmail.com"} + {"name": "Jessé LIBEN", "email": "jliben@gmail.com"}, + {"name": "Daniel Amado", "email": "velkan14@gmail.com"} ], "license": "ISC", "bugs": { diff --git a/src/error.js b/src/error.js index cfc6494..5ad9486 100644 --- a/src/error.js +++ b/src/error.js @@ -24,6 +24,16 @@ export const assign = (stream, err) => { return stream } +// Just append the buffer to the stream. The error is created before emitting on close +export const append = (stream, buffer) => { + if (stream.stderr) { + stream.stderr += buffer + } else { + stream.stderr = buffer + } + return stream +} + export const fromBuffer = chunk => { const stderr = chunk.toString() const match = stderr.match(ERROR) @@ -36,4 +46,4 @@ export const fromBuffer = chunk => { return err } -export default { assign, fromBuffer } +export default { append, assign, fromBuffer } diff --git a/src/events.js b/src/events.js index 969b876..ff73d10 100644 --- a/src/events.js +++ b/src/events.js @@ -23,9 +23,8 @@ export const onErrorFactory = ({ Err }) => (stream, err) => { } export const onStderrFactory = ({ Err }) => (stream, buffer) => { - const err = Err.fromBuffer(buffer) - Err.assign(stream, err) - debug('error: from stderr: %O', err) + Err.append(stream, buffer) + debug('error: append from stderr: %O', buffer) return stream } @@ -88,7 +87,12 @@ export const onStdoutFactory = ({ Maybe }) => (stream, chunk) => { return stream } -export const onEndFactory = () => (stream) => { +export const onEndFactory = ({ Err }) => (stream) => { + if (stream.stderr) { + const err = Err.fromBuffer(stream.stderr) + Err.assign(stream, err) + debug('error: from stderr: %O', err) + } if (stream.err) { stream.emit('error', stream.err) } diff --git a/src/main.js b/src/main.js index 3013653..d5e6967 100644 --- a/src/main.js +++ b/src/main.js @@ -30,7 +30,7 @@ const listenFactory = ({ Lifecycle, Err, Maybe }) => seven => { errorHandler: onErrorFactory({ Err }), stderrHandler: onStderrFactory({ Err }), stdoutHandler: onStdoutFactory({ Maybe }), - endHandler: onEndFactory() + endHandler: onEndFactory({ Err }) })(seven) return seven } diff --git a/test/unit/events.spec.js b/test/unit/events.spec.js index 5b4a607..771b160 100644 --- a/test/unit/events.spec.js +++ b/test/unit/events.spec.js @@ -22,19 +22,16 @@ describe('Unit: events.js', function () { describe('onStderrFactory()', function () { it('should operate on stream and error', function () { - const sevenFake = { testprop: null } + const sevenFake = {} const bufferFake = Buffer.from('42') const ErrFake = { - assign: (stream, err) => { - stream.testprop = err - }, - fromBuffer: (buffer) => { - return buffer.toString() + append: (stream, err) => { + stream.stderr = err } } const onError = Events.onStderrFactory({ Err: ErrFake }) onError(sevenFake, bufferFake) - expect(sevenFake.testprop).to.eql('42') + expect(sevenFake.stderr).to.eql(bufferFake) }) }) @@ -71,7 +68,8 @@ describe('Unit: events.js', function () { it('should flow the normal way', function () { // 1. setup const sevenFake = { - _stage: STAGE_HEADERS + _stage: STAGE_HEADERS, + _isProgressFlag: true } // 2. run const onStdout = Events.onStdoutFactory({ Maybe }) @@ -98,7 +96,8 @@ describe('Unit: events.js', function () { // 1. setup const sevenFake = { _stage: STAGE_HEADERS, - _dataType: 'symbol' + _dataType: 'symbol', + _isProgressFlag: true } // 2. run const onStdout = Events.onStdoutFactory({ Maybe }) @@ -124,8 +123,20 @@ describe('Unit: events.js', function () { }) describe('onEndFactory()', function () { + const ErrFake = { + append: (stream, err) => { + stream.stderr = err + }, + assign: (stream, err) => { + stream.testprop = err + }, + fromBuffer: (buffer) => { + return buffer.toString() + } + } + it('should emit error and close given error in process', function (done) { - const onEnd = Events.onEndFactory() + const onEnd = Events.onEndFactory({ ErrFake }) const sevenFake = new Readable({ read () {} }) const errFake = new Error('FakeError') let counterError = 0 @@ -142,7 +153,7 @@ describe('Unit: events.js', function () { }) it('should emit end event', function (done) { - const onEnd = Events.onEndFactory() + const onEnd = Events.onEndFactory({ ErrFake }) const sevenFake = new Readable({ read () {} }) let counterClose = 0 sevenFake.on('end', () => { ++counterClose })