首页 > 解决方案 > Javascript管道没有捕获我抛出的错误

问题描述

我有一个管道,在其中一个转换中我抛出了一个错误,我想以某种方式捕获它。

_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();
}

在我的单元测试中,我想检查某些东西实际上在它不应该有的地方不起作用。

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();
                }
            }
    );        
});

不幸的是,没有 .on('error' 和 err => 函数正在启动,我得到 Uncaught 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)

该怎么办?

标签: javascripterror-handlingpipeline

解决方案


如果错误与流相关,您需要分别error为读取和写入流添加事件处理程序

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));

请记住,如果您有更多流,则必须分别处理每个流。


推荐阅读