From f8c617dbe2690735511d5e8d4e73138458d808cb Mon Sep 17 00:00:00 2001 From: cjihrig Date: Wed, 19 Apr 2017 15:01:43 -0400 Subject: [PATCH] stream: improve multiple callback error message When a transform stream's callback is called more than once, an error is emitted with a somewhat confusing message. This commit hopes to improve the quality of the error message. Fixes: https://github.com/nodejs/node/issues/12513 PR-URL: https://github.com/nodejs/node/pull/12520 Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- lib/_stream_transform.js | 6 ++++-- .../test-stream-transform-callback-twice.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-stream-transform-callback-twice.js diff --git a/lib/_stream_transform.js b/lib/_stream_transform.js index 63f65f34ce41f9..1aa423fffa75fe 100644 --- a/lib/_stream_transform.js +++ b/lib/_stream_transform.js @@ -88,8 +88,10 @@ function afterTransform(stream, er, data) { var cb = ts.writecb; - if (!cb) - return stream.emit('error', new Error('no writecb in Transform class')); + if (!cb) { + return stream.emit('error', + new Error('write callback called multiple times')); + } ts.writechunk = null; ts.writecb = null; diff --git a/test/parallel/test-stream-transform-callback-twice.js b/test/parallel/test-stream-transform-callback-twice.js new file mode 100644 index 00000000000000..31a4aea29e5f65 --- /dev/null +++ b/test/parallel/test-stream-transform-callback-twice.js @@ -0,0 +1,14 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const { Transform } = require('stream'); +const stream = new Transform({ + transform(chunk, enc, cb) { cb(); cb(); } +}); + +stream.on('error', common.mustCall((err) => { + assert.strictEqual(err.toString(), + 'Error: write callback called multiple times'); +})); + +stream.write('foo');