I have a pipeline and in one of the transforms I throw an error, which I want to catch somehow.
_transform(chunk,encoding,done) {
let strChunk = decoder.write(chunk);
if(strChunk === '\u0003')
process.exit();
let felem = JSON.parse(strChunk);
let seq = felem.seq.replace(/\s+/g, '');
this.type = "nucleotide";
for(let i=0; i<this.checkMax && i<seq.length; i++) {
let c = seq[i];
if(!(c in this.nucleotides) && (c in this.aminoacids)) {
this.type = "aminoacid";
felem["type"]=this.type;
if((this.forward === "both") || (this.forward === this.type))
this.push(JSON.stringify(felem));
break;
} else {
if(!(c in this.nucleotides) && !(c in this.aminoacids) && !(c in this.permissables)) {
throw new Error("Unexpected character in sequence (" + felem.def + "): " + c + ".");
}
}
}
done();
}
In my unit test, I would like to check that something actually didn't work where it shouldn't have.
it('Should throw an error.', function(done) {
this.timeout(5000);
let fe = new FASTAElement(null).on('error', function(err) {
console.log("fe",err);
});
let fst = new FASTASeqType(null).on('error', function(err) {
console.log("fst",err);
});
let inStream = fs.createReadStream("../files/bad.fasta").on('error', function(err) {
console.log("inStream",err);
});
let outStream = fs.createWriteStream("../files/bad_typed.fasta").on('error', function(err) {
console.log("outStream",err);
});
let fw = new FASTAWriter(null,80).on('error', function(err) {
console.log("fw",err);
});
let sp = new split2().on('error', function(err) {
console.log("sp",err);
});
pipeline(
inStream,
sp,
fe,
fst,
fw,
outStream,
err => {
if(err) {
console.log(err);
done();
} else {
assert("true",false);
done();
}
}
);
});
Unfortunately, non of the .on('error' nor err => functions are kicking in and I get Uncaught Error: ... plus the stack trace: The first line refers to the position where I throw the error.
at FASTASeqType._transform (h:\Code\ios\src\FASTA\FASTASeqType.js:41:27)
at FASTASeqType.Transform._read (_stream_transform.js:191:10)
at FASTASeqType.Transform._write (_stream_transform.js:179:12)
at doWrite (_stream_writable.js:403:12)
at writeOrBuffer (_stream_writable.js:387:5)
at FASTASeqType.Writable.write (_stream_writable.js:318:11)
at FASTAElement.ondata (_stream_readable.js:695:22)
at addChunk (_stream_readable.js:286:12)
at readableAddChunk (_stream_readable.js:268:9)
at FASTAElement.Readable.push (_stream_readable.js:209:10)
at FASTAElement.Transform.push (_stream_transform.js:152:32)
at FASTAElement._transform (h:\Code\ios\src\FASTA\FASTAElement.js:27:22)
at FASTAElement.Transform._read (_stream_transform.js:191:10)
at FASTAElement.Transform._write (_stream_transform.js:179:12)
at doWrite (_stream_writable.js:403:12)
at writeOrBuffer (_stream_writable.js:387:5)
at FASTAElement.Writable.write (_stream_writable.js:318:11)
at Transform.ondata (h:\Code\ios\src\node_modules\readable-stream\lib\_stream_readable.js:681:20)
at addChunk (h:\Code\ios\src\node_modules\readable-stream\lib\_stream_readable.js:298:12)
at readableAddChunk (h:\Code\ios\src\node_modules\readable-stream\lib\_stream_readable.js:280:11)
at Transform.Readable.push (h:\Code\ios\src\node_modules\readable-stream\lib\_stream_readable.js:241:10)
at Transform.push (h:\Code\ios\src\node_modules\readable-stream\lib\_stream_transform.js:139:32)
at push (h:\Code\ios\src\node_modules\split2\index.js:65:10)
at Transform.transform [as _transform] (h:\Code\ios\src\node_modules\split2\index.js:43:5)
at Transform._read (h:\Code\ios\src\node_modules\readable-stream\lib\_stream_transform.js:177:10)
at Transform._write (h:\Code\ios\src\node_modules\readable-stream\lib\_stream_transform.js:164:83)
at doWrite (h:\Code\ios\src\node_modules\readable-stream\lib\_stream_writable.js:409:139)
at writeOrBuffer (h:\Code\ios\src\node_modules\readable-stream\lib\_stream_writable.js:398:5)
at Transform.Writable.write (h:\Code\ios\src\node_modules\readable-stream\lib\_stream_writable.js:307:11)
at ReadStream.ondata (_stream_readable.js:695:22)
at addChunk (_stream_readable.js:286:12)
at readableAddChunk (_stream_readable.js:268:9)
at ReadStream.Readable.push (_stream_readable.js:209:10)
at internal/fs/streams.js:210:12
at FSReqCallback.wrapper [as oncomplete] (fs.js:488:5)
What to do?