0

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?

jallmer
  • 589
  • 3
  • 17
  • Just wrapped everything in try .. catch. No change. Can it have anything to do with using mocha? – jallmer May 20 '21 at 06:52

1 Answers1

1

If error is stream related, You need to add error event handler for both read and write streams separately

let inStream = fs.createReadStream("../files/bad.fasta").on('error', (err) => console.log(err));
let outStream = fs.createWriteStream("../files/bad_typed.fasta").on('error', (err) => console.log(err));

Keep in mind if you have more streams, Each of them must be handled separately.

omidh
  • 2,526
  • 2
  • 20
  • 37
  • I applied the suggestion (see code edits). Still, none of the .on error handlers are called. – jallmer May 20 '21 at 06:43
  • Have you tried `expect(pipeline(...)).to.throw();`? – omidh May 20 '21 at 09:16
  • I pulled out pipeline and named it `function pip() {pipeline(...);}`. Then I did the `expect(pip).to.throw("...")`; The Uncaught error message is gone, but the error is still not caught anywhere and I get: `AssertionError: expected [Function: pip] to throw an error` from mocha. – jallmer May 20 '21 at 11:07
  • You need to call it, Try `expect(pip(args)).to.throw("...")` instead – omidh May 20 '21 at 11:22
  • When I put `expect(pip()).to.throw("...");` I get: ` AssertionError: expected undefined to be a function`. So I think chai wants to call the function itself. See: https://stackoverflow.com/questions/21587122/mocha-chai-expect-to-throw-not-catching-thrown-errors?rq=1 – jallmer May 20 '21 at 11:48