Skip to content

Commit

Permalink
for now giving up to handle transform stream errors
Browse files Browse the repository at this point in the history
  • Loading branch information
thlorenz committed Dec 8, 2013
1 parent 83ec090 commit e4bfc2f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
34 changes: 23 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,27 @@ function ensureNoStringDecode(s) {
}
}

function transformStream(transforms) {
function transformStream(progress, transforms) {
function onerror(err) {
progress.emit('error', err);
}

return transforms && transforms.length
? function (file) {
var streams = transforms.map(function (t) {
var s = t(file)
var s = t(file);
ensureNoStringDecode(s);
return s;
return s.on('error', onerror);
});
var combined = combine.apply(null, streams);

var combined = combine.apply(null, streams).on('error', onerror);
return combined;
}
: function (file) { return through() };
}

function transformContent(transforms) {
var ts = transformStream(transforms);
function transformContent(progress, transforms) {
var ts = transformStream(progress, transforms);
return function (entry, enc, cb) {
var self = this;

Expand Down Expand Up @@ -123,7 +128,7 @@ var go = module.exports = function(mutinyopts, readopts) {
.on('error', progress.emit.bind(progress, 'error'))
.pipe(through({ objectMode: true }, outForFiles(getOutStream, outdir)))
.on('error', progress.emit.bind(progress, 'error'))
.pipe(through({ objectMode: true }, transformContent(transforms)))
.pipe(through({ objectMode: true }, transformContent(progress, transforms)))
.on('error', progress.emit.bind(progress, 'error'))
.on('end', progress.emit.bind(progress, 'end'))
.pipe(progress);
Expand All @@ -138,7 +143,16 @@ function inspect(obj, depth) {
function toUpper(file, content) {
return through(
function (chunk, enc, cb) {
console.log('encoding', enc);
this.push(chunk.toUpperCase());
cb();
}
)
}

function toUpperError(file, content, cb) {
return through(
function (chunk, enc, cb) {
if ((/two/mg).test(chunk)) return cb(new Error('I hate to be number two!'));
this.push(chunk.toUpperCase());
cb();
}
Expand All @@ -149,7 +163,6 @@ function trimLeading(file, content, cb) {
var data = '';

function ondata(chunk, enc, cb) {
console.log('trim-encoding', enc);
data += chunk;
cb();
}
Expand All @@ -172,8 +185,7 @@ if (!module.parent && typeof window === 'undefined') {
var root = path.join(fixtures, 'root');
var outdir = path.join(fixtures, 'out');


go({ transforms: [ trimLeading, toUpper ], getOutStream: getStdOut, outdir: outdir }, { root: root })
go({ transforms: [ toUpperError ], getOutStream: getStdOut, outdir: outdir }, { root: root })
.on('data', function (data) {
console.log(data);
})
Expand Down
9 changes: 7 additions & 2 deletions test/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ function toUpperError(file, content, cb) {
cb();
}
)
// definitely handling it here, so why does it go undhandled ???
// see test at bottom
.on('error', function (err) {
console.error('E', err);
});
}

function trimLeading(file, content, cb) {
Expand Down Expand Up @@ -102,7 +107,6 @@ test('\nrunning toUpper and then trimLeading transforms', function (t) {
test('\nrunning trimLeading and then toUpper transforms', function (t) {
var progress = []

// TODO: passing transforms in opposite order fails due to decodeStrings not properly applying -- no clue why
mutiny({ getOutStream: getStdOut, transforms: [ trimLeading, toUpper ] }, { root: root })
.on('error', fail.bind(t))
.on('data', [].push.bind(progress))
Expand All @@ -122,6 +126,7 @@ test('\nrunning trimLeading and then toUpper transforms', function (t) {
})


// TODO: Not sure why these errors go unhandled even though now I'm actually handling it right when creating the transform
/*test('\nrunning error prone upperCase and trimLeading transforms', function (t) {
var progress = []
Expand All @@ -131,5 +136,5 @@ test('\nrunning trimLeading and then toUpper transforms', function (t) {
t.end()
})
.on('data', [].push.bind(progress))
.on('end', fail.bind(t));
.on('end', fail.bind(null, t));
})*/

0 comments on commit e4bfc2f

Please sign in to comment.